MongoDB and Java driver: "ignore case" in request

This is the code I'm using now, how do I add an attribute "ignore case"?

DBObject query = new BasicDBObject("prop", value); 

thanks

+7
java case-insensitive mongodb
source share
5 answers

When I had the exact problem, I could not request, ignoring the case. I ended up copying the value I wanted to find in order to normalize it. In this case, you can create a new property and convert it to lowercase and have an index.

EDIT:

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

I wonder if this works?

+14
source share

In case you use Spring -java below, this is the way you can perform case insensitive searches.

 public List<Discussion> searchQuestionByText(String qText){ Query query = new Query(); query.addCriteria(Criteria.where("discussionName").regex(qText,"i")); return mongoTemplate.find(query, Discussion.class); } 
+3
source share
 db.iolog.find({$where:"this.firstname.toLowerCase()==\"telMan\".toLowerCase()"}); DBObject ref = new BasicDBObject(); ref.append("firstname", new BasicDBObject("$where","this.firstname.toLowerCase()=="+firstname+".toLowerCase()")); 
0
source share

I also tried to make the best use of the case-insensitive implementation. If you are using MongoDB Java driver 3.0 or later, you should use this following code with the $ parameter option! It is really easy to use:

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

The fields "key" and "value" must be changed using your own data.

(I also advise you to search using the $ regex parameter to obtain information by partially matching if there are more and more records in the database.)

0
source share
  mapValue = new HashMap<String, Object>(); mapValue.put("$options", "i"); mapValue.put("$regex", "smth"); searchMap.put("name", mapValue); collection.find(new BasicDBObject(searchMap)); 
-one
source share

All Articles