sunsynd
sunsynd

Reputation: 31

Spring Data mongodb like query

I want to make a text search case insensitive with regex query with spring-data mongo .

For example in SQL:

SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

How can i make this query with spring-data mongo using MongoRepository Query DSL ?

Thanks in advance

Upvotes: 1

Views: 1416

Answers (2)

Mehedi Hasan Shifat
Mehedi Hasan Shifat

Reputation: 720

Here is the raw mongo query for your sql :

db.Customers.find( { CustomerName: /^a/ } )

or

db.Customers.find( { CustomerName: { $regex: /^a/ } } )

Upvotes: 2

khoa junior
khoa junior

Reputation: 29

public interface ProductRepository extends MongoRepository<Product, String> {

@Query("{$or : [{'name': { $regex: ?0, $options:'i' }}, {'description': { $regex: ?0, $options:'i' }}]}")
List<Product> findProductByRegexString(final String regexString);

you can use query like that hope help you

Upvotes: 0

Related Questions