Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression Spring data mongodb repositories

Good morning,

I´m trying to combine regular expression with Spring data mongodb repository using Query annotation. What I want is search one substring inside one string attribute of my mongo document. I have been looking in google and here, but I did not find anything elegant, and I was wondering if Spring data has something official about this using the repositories.

Regards.

like image 238
paul Avatar asked May 23 '13 07:05

paul


People also ask

Can we use spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.

How MongoDB read data in spring boot?

Read operations using Spring Boot MongoDBFetch all the documents (grocery items) using the findAll() method. Get a single item (document) by its name field using the findItemByName method. Get a list of items based on a category.

What is @document annotation in spring boot?

@Document is an annotation provided by Spring data project. It is used to identify a domain object, which is persisted to MongoDB. So you can use it to map a Java class into a collection inside MongoDB. If you don't use Spring Data, you don't need this annotation.


1 Answers

It seems like an old question, so maybe you've already had a solution but here how I handled the same issue :

@Query(value = "{'title': {$regex : ?0, $options: 'i'}}")
Foo findByTitleRegex(String regexString);

using the /?0/ notation won't work since Spring Data places a String value with quotes

like image 170
Alper Avatar answered Sep 19 '22 12:09

Alper