2

I know make case-insensitive query in console could use regex like db.stuff.find( { foo: /bar/i } );

MongoDB: Is it possible to make a case-insensitive query?

But I cannot fiure out how to use it in java drive.example:

DBObject q = BasicDBObjectBuilder.start().add(type, s).add("user", user).get();
DBCursor cursor = c.find(q);

I want user case-insensitive,How to do that?

Community
  • 1
  • 1
Shisoft
  • 4,197
  • 7
  • 44
  • 61

2 Answers2

3

You can do it using Pattern.compile() where the first param is the value you are querying for and the second param is Pattern.CASE_INSENSITIVE as below.

query.put("db.variable", Pattern.compile(searchValueHere, Pattern.CASE_INSENSITIVE));
stock
  • 117
  • 5
0

You are already aware of using regular expression. The only other option I can think of would be to normalize the data to be queried.

Bill
  • 599
  • 4
  • 2