【发布时间】:2010-11-01 13:37:44
【问题描述】:
这是我现在使用的代码,如何添加“忽略大小写”属性?
DBObject query = new BasicDBObject("prop", value);
谢谢
【问题讨论】:
标签: java mongodb case-insensitive
这是我现在使用的代码,如何添加“忽略大小写”属性?
DBObject query = new BasicDBObject("prop", value);
谢谢
【问题讨论】:
标签: java mongodb case-insensitive
当我遇到确切的问题时,我无法通过忽略大小写来进行查询。我最终复制了我想要搜索的值对其进行规范化。在这种情况下,您可以创建一个新属性并将其转换为小写并在其上创建一个索引。
编辑:
DBObject ref = new BasicDBObject();
ref.put("myfield", Pattern.compile(".*myValue.*" , Pattern.CASE_INSENSITIVE));
DBCursor cur = coll.find(ref);
我想知道这是否有效?
【讨论】:
如果你使用 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);
}
【讨论】:
我还试图充分利用“不区分大小写”的实现。 如果您使用MongoDB Java driver 3.0 或更高版本,则应使用以下代码,并带有 $option 参数!它真的很容易使用:
Document basicQuery = new Document();
basicQuery.append("key", new Document("$regex","value").append("$options","i"));
“key”和“value”字段需要用自己的数据来改变。
(我还建议您使用 $regex 参数进行搜索,以便通过部分匹配检索信息,以防数据库中的记录越来越多)。
【讨论】:
db.iolog.find({$where:"this.firstname.toLowerCase()==\"telMan\".toLowerCase()"});
DBObject ref = new BasicDBObject();
ref.append("firstname", new BasicDBObject("$where","this.firstname.toLowerCase()=="+firstname+".toLowerCase()"));
【讨论】:
mapValue = new HashMap<String, Object>();
mapValue.put("$options", "i");
mapValue.put("$regex", "smth");
searchMap.put("name", mapValue);
collection.find(new BasicDBObject(searchMap));
【讨论】: