Spring Data mongo case insensitive like query
Asked Answered
G

7

28

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

Gerik answered 19/1, 2017 at 16:3 Comment(0)
I
63

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);
Inquisitorial answered 19/1, 2017 at 18:42 Comment(0)
V
26

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

Virgulate answered 28/4, 2017 at 0:53 Comment(1)
If your regex is 'test', this will return documents with titles like: test1, test2 etc.Apt
B
6
@Repository
public interface CompetenceRepository extends MongoRepository<Competence, String> {

    @Query("{ 'titre' : { '$regex' : ?0 , $options: 'i'}}")
    List<Competence> findAllByTitreLikeMotcle(String motCle);
}

Should works perfectly ,it works in my projects.for words in French too

Brail answered 7/1, 2018 at 10:52 Comment(0)
R
1

In repository pattern I tried this and worked

@Query("{ 'username' : ?0 }.collation( { locale: 'en', strength: 2 } )")

User findByUserName(String name);

Reducer answered 6/11, 2020 at 6:43 Comment(0)
U
0

Yes, you can, provided you have set up it right, I would add something like this in your repository method:

{'username': {$regex: /ab/i }})

@Query("{'username': {$regex: /?1/i }})")

List findUsersByUserName(String userName);

Universally answered 19/1, 2017 at 18:25 Comment(0)
M
0

For like query on numeric fields (int or double)

db.players.find({
  $and: [{
      "type": {
        $in: ["football"]
      }
    },
    {
      $where : "/^250.*/.test(this.salary)"
    }]
})

See the detailed code: Spring Boot mongodb like query on int/double using mongotemplate

Mckelvey answered 12/12, 2019 at 9:52 Comment(0)
D
0
String tagName = "apple";

Query query = new Query();
query.limit(10);
query.addCriteria(Criteria.where("tagName").regex(tagName));

mongoOperation.find(query, Tags.class);

Reference - https://mkyong.com/mongodb/spring-data-mongodb-like-query-example/

Delrio answered 29/8, 2020 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.