Parth Solanki
Parth Solanki

Reputation: 3448

Case insensitive search in Mongodb

I am trying a case insensitive search in Mongo. Basically I want case insensitive string match I am using regex. Here is my code

Query query = new Query( Criteria.where(propName).regex(value.toString(), "i"));

But the above dosent match my whole string(a string sometime with spaces). It returns values even if its a substring.

Eg: Suppose my collection has 2 values "Bill" and "Bill status',It returns me "bill" even if my search is "bill status". It returns results even if the there is a sub string of the string I am searching for

I tried , Query query = new Query( Criteria.where(propName).is(value.toString()));

But the above is case sensitive. Can someone please help on this.

Upvotes: 0

Views: 2465

Answers (1)

ry_donahue
ry_donahue

Reputation: 505

Your regex isn't pinned to front and back for equivalence. Pass the value in like:

foo: /^VALUE$/i

The caret pins the string to the front for full text match with case insensitivity.

Upvotes: 1

Related Questions