mogodb不区分大小写查询

版权声明:转发请注明,谢谢配合 https://blog.csdn.net/qq_31289187/article/details/82996047

菜鸟教程-mogodb学习

一、不区分大小写的正则表达式

如果检索需要不区分大小写,我们可以设置 $options 为 $i。

以下命令将查找不区分大小写的字符串 runoob:

>db.posts.find({post_text:{$regex:"runoob",$options:"$i"}})

集合中会返回所有包含字符串 runoob 的数据,且不区分大小写:

{
   "_id" : ObjectId("53493d37d852429c10000004"),
   "post_text" : "hey! this is my post on  runoob", 
   "tags" : [ "runoob" ]
} 

二、有个正则表达式,java代码就好写了

我在springboot项目使用mogodb

1、引入jar包

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
			<version>2.0.1.RELEASE</version>
		</dependency>

2、引入MongoTemplate对象

@Autowired
private MongoTemplate mongoTemplate;

3、不分区大小写查询,其中操作符“i”:表示不分区大小写

JSONObject ethInfo = mongoTemplate.findOne(new Query(Criteria.where("car_brand_type").regex(ethAddress,"i")), JSONObject.class, "car_info");

猜你喜欢

转载自blog.csdn.net/qq_31289187/article/details/82996047