简体   繁体   English

如何使用 Criteria、MongoTemplate 和 Java 搜索不区分大小写的值?

[英]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 ?如何使此搜索同时适用于car_color = BLUEcar_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).我只想更新car_color变量中匹配的car_color (不区分大小写)。

I think what you want is to pre process your string to be all caps or all lowercase.我认为您想要的是将您的字符串预处理为全部大写或全部小写。 In java that's done like:在 java 中这样做是这样的:

car_color.toLowerCase() or car_color.toUpperCase() car_color.toLowerCase()car_color.toUpperCase()

Then you know its all in caps or all in lowercase, and can compare the string appropriately然后你知道它全部是大写还是全部小写,并且可以适当地比较字符串

The solution for my problem is:我的问题的解决方案是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM