Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 618
paul Avatar asked May 24 '13 07:05

paul


People also ask

What is MongoRepository?

MongoRepository is an interface provided by Spring Data in the package org. springframework. data. mongodb.

What is Spring 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.

Can we use JPA with MongoDB?

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).


1 Answers

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);
like image 81
Maciej Walkowiak Avatar answered Nov 12 '22 10:11

Maciej Walkowiak