Search code examples
javaspringspring-data-mongodbcriteriacase-insensitive

How search case insensitive values using Criteria, MongoTemplate and Java?


I'm currently doing:

criteria = new Criteria().andOperator(where("car.color").is(car_color),
                                      where("car_size").is(car_size))

How to make this search works for both car_color = BLUE and car_color = blue ?


Update:

This partially works:

where("car.color").regex(car_color, "i"),

But then all values below would be updated:

BLUE
BBLUE
blue
bluee

And I want to update only what is matched in the car_color variable(case insensitive).


Solution

  • The solution for my problem is:

    criteria = new Criteria().andOperator(where("car.color").regex("^" + car_color+ "$", "i"),
                                          where("car_size").is(car_size))