简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM