paul
paul

Reputation: 13481

spring MongoRepository Query regex

I´m looking to use a regular expresion using MongoRepository inside a Query annotation. The only information that I found so far is a Chinese post but do not explain how make it works, and I´m not sure if is what I´m looking for.

@Query("{ 'name':{'$regex':?2,'$options':'i'}, sales': {'$gte':?1,'$lte':?2}}") public Page findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);

Somebody know if it possible use a specific regex in the query?

Regards.

Upvotes: 9

Views: 13749

Answers (1)

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

i toggles case insensitivity, and allows all letters in the pattern to match upper and lower cases.

Read more about $regex in MongoDB docs.

In your query it looks like you have mixed up something with parameters order (?2 in name regex). I believe it should be:

@Query("{ 'name':{$regex:?0,$options:'i'}, 'sales': {$gte:?1,$lte:?2}}") 
public Page findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);

Upvotes: 16

Related Questions