Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data mongo case insensitive like query

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

For example in Oracle :

select * from user where lower(username) like '%ab%'

How can i make this query with spring-data mongo ?

Thanks in advance

like image 744
akinKaplanoglu Avatar asked Jan 19 '17 16:01

akinKaplanoglu


People also ask

How do I make case insensitive queries on MongoDB?

First, you can run a case-insensitive query using $regex with the i option. These queries will give you the expected case-insensitive results. However, queries that use $regex cannot efficiently utilize case-insensitive indexes, so these queries can be very slow depending on how much data is in your collection.

How do you make a case insensitive in spring boot?

Here, the IgnoreCase keyword ensures that the query matches are case insensitive. Despite having passed “FrED” as the argument, our returned list of passengers contains a Passenger with the firstName as “Fred”. Clearly, with the help of the IgnoreCase keyword, we have achieved a case insensitive match.

Is MongoDB data case-sensitive?

As mentioned by @natac13 and @007_jb mongo shell is an interactive javascript interpreter and hence it is also case-sensitive.

How do I ignore a case in JPQL?

2.2 Like or Contains with ignoring case JPA QueryUsing IgnoreCase in the method name, we can ignore case while fetching data. To check case-insensitive lower function is useful, here we have lower([column_name]) and parameter has been lower while before passing to query. Example: findByEmployeeNameContaining("Jone".


2 Answers

You can try something like below. Assumes you have a User pojo class.

Using MongoTemplate

i option for case insensitive:

Criteria regex = Criteria.where("username").regex(".*ab.*", "i");      
mongoOperations.find(new Query().addCriteria(regex), User.class);

Using MongoRepository (Case sensitive)

List<User> users = userRepository.findByUserNameRegex(".*ab.*");

interface UserRepository extends MongoRepository<User, String> {
     List<User> findByUserNameRegex(String userName);
}

Using MongoRepository with Query dsl (Case sensitive)

List<User> users = userRepository.findByQuery(".*ab.*");

interface UserRepository extends MongoRepository<User, String> {
     @Query("{'username': {$regex: ?0 }})")
     List<User> findByQuery(String userName);
}

For Non-Regex based query you can now utilize case insensitive search/sort through collation with locale and strength set to primary or secondary:

Query query = new Query(filter);
query.collation(Collation.of("en").
                  strength(Collation.ComparisonLevel.secondary()));
mongoTemplate.find(query,clazz,collection);
like image 126
s7vr Avatar answered Sep 18 '22 14:09

s7vr


I know this is an old question. I just found the solution in another post. Use $regex and $options as below:

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

see the original answer: https://stackoverflow.com/a/19068401

like image 39
Arthur Kazemi Avatar answered Sep 18 '22 14:09

Arthur Kazemi