Shisoft
Shisoft

Reputation: 4245

How to make a case-insensitive query in java?

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?

Upvotes: 2

Views: 4105

Answers (2)

stock
stock

Reputation: 117

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

Upvotes: 3

Bill
Bill

Reputation: 599

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.

Upvotes: 0

Related Questions