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.
MongoRepository is an interface provided by Spring Data in the package org. springframework. data. mongodb.
Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.
JPA Entity Classes The EntityManager instance associated with the persistence context supplies CRUD operations that can be utilised to interact to and from the Java application and database (in our case, mongodb).
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With