MongoDB 和 Java 驱动程序:“忽略大小写”查询中
这是我现在使用的代码,如何添加“忽略大小写”属性?
DBObject query = new BasicDBObject("prop", value);
谢谢
This is the code I'm using now, how do I add the "ignore case" attribute?
DBObject query = new BasicDBObject("prop", value);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当我遇到确切的问题时,我无法通过忽略大小写来查询。我最终复制了我想要搜索的值并将其标准化。在这种情况下,您可以创建一个新属性并将其转换为小写并在其上建立索引。
编辑:
我想知道这是否有效?
When I had the exact problem, I wasn't able to query by ignoring case. I ended up copy the value that I wanted to search normalizing it. In this case, you can create a new property and convert it to lower case and have an index on that.
EDIT:
I wonder if that works?
如果您使用下面提到的 Spring-java,您可以以不区分大小写的方式进行搜索。
In case if you are using Spring-java below mentioned is the way you can make it search in case-insensitive manner.
我还试图充分利用“不区分大小写”的实现。
如果您使用 MongoDB Java 驱动程序 3.0 或更高版本,则应使用以下内容代码,带有 $option 参数!它真的很容易使用:
字段“key”和“value”需要用您自己的数据进行更改。
(我还建议您使用 $regex 参数进行搜索,以便在数据库中拥有越来越多记录的情况下通过部分匹配检索信息)。
I was also trying to get the best use of the implementation "case insensitive".
If you're using the MongoDB Java driver 3.0 or later, you should use this following code, with the $option parameter ! It's really easy to use:
The fields "key" and "value" need to be changed with your own data.
(I also advise you to search with $regex parameter in order to retrieve information via partial matching in the case you would have more and more records in the database).