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

Time:11-04

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).

CodePudding user response:

I think what you want is to pre process your string to be all caps or all lowercase. In java that's done like:

car_color.toLowerCase() or car_color.toUpperCase()

Then you know its all in caps or all in lowercase, and can compare the string appropriately

CodePudding user response:

The solution for my problem is:

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