Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringMongo Case insensitive search regex

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.

like image 643
Droidme Avatar asked Jul 31 '12 14:07

Droidme


1 Answers

Insensitive search using regex search. input_location could be Delhi,delhi,DELHI, it works for all.

Criteria.where("location").regex( Pattern.compile(input_location, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)));   
like image 131
Ajay Kumar Avatar answered Oct 20 '22 01:10

Ajay Kumar