MongoDB 和 Java 驱动程序:“忽略大小写”查询中

发布于 2024-09-29 23:06:05 字数 120 浏览 1 评论 0原文

这是我现在使用的代码,如何添加“忽略大小写”属性?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

始终不够爱げ你 2024-10-06 23:06:05

当我遇到确切的问题时,我无法通过忽略大小写来查询。我最终复制了我想要搜索的值并将其标准化。在这种情况下,您可以创建一个新属性并将其转换为小写并在其上建立索引。

编辑:

DBObject ref = new BasicDBObject();
ref.put("myfield", Pattern.compile(".*myValue.*" , Pattern.CASE_INSENSITIVE));
DBCursor cur = coll.find(ref); 

我想知道这是否有效?

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:

DBObject ref = new BasicDBObject();
ref.put("myfield", Pattern.compile(".*myValue.*" , Pattern.CASE_INSENSITIVE));
DBCursor cur = coll.find(ref); 

I wonder if that works?

執念 2024-10-06 23:06:05

如果您使用下面提到的 Spring-java,您可以以不区分大小写的方式进行搜索。

public List<Discussion> searchQuestionByText(String qText){
        Query query = new Query();
        query.addCriteria(Criteria.where("discussionName").regex(qText,"i"));
        return mongoTemplate.find(query, Discussion.class);     
    }

In case if you are using Spring-java below mentioned is the way you can make it search in case-insensitive manner.

public List<Discussion> searchQuestionByText(String qText){
        Query query = new Query();
        query.addCriteria(Criteria.where("discussionName").regex(qText,"i"));
        return mongoTemplate.find(query, Discussion.class);     
    }
等数载,海棠开 2024-10-06 23:06:05

我还试图充分利用“不区分大小写”的实现。
如果您使用 MongoDB Java 驱动程序 3.0 或更高版本,则应使用以下内容代码,带有 $option 参数!它真的很容易使用:

Document basicQuery = new Document();
basicQuery.append("key", new Document("$regex","value").append("$options","i")); 

字段“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:

Document basicQuery = new Document();
basicQuery.append("key", new Document("$regex","value").append("$options","i")); 

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).

<逆流佳人身旁 2024-10-06 23:06:05
db.iolog.find({$where:"this.firstname.toLowerCase()==\"telMan\".toLowerCase()"});
DBObject ref = new BasicDBObject();
ref.append("firstname", new BasicDBObject("$where","this.firstname.toLowerCase()=="+firstname+".toLowerCase()"));
db.iolog.find({$where:"this.firstname.toLowerCase()==\"telMan\".toLowerCase()"});
DBObject ref = new BasicDBObject();
ref.append("firstname", new BasicDBObject("$where","this.firstname.toLowerCase()=="+firstname+".toLowerCase()"));
夕色琉璃 2024-10-06 23:06:05
 mapValue = new HashMap<String, Object>();
 mapValue.put("$options", "i");
 mapValue.put("$regex", "smth");
 searchMap.put("name", mapValue);
 collection.find(new BasicDBObject(searchMap));
 mapValue = new HashMap<String, Object>();
 mapValue.put("$options", "i");
 mapValue.put("$regex", "smth");
 searchMap.put("name", mapValue);
 collection.find(new BasicDBObject(searchMap));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文