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.
Yes, DataNucleus JPA allows it, as well as to many other databases.
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.
@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.
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
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