2022MongoDB4-find正则等其它用法

2022/1/10 19:07:56

本文主要是介绍2022MongoDB4-find正则等其它用法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1查看name包括着3个人的所有数据:

$in:可以使用索引,但是$nin不能使用索引,不能使用索引的尽量不用或者少用

> db.users.find({"name":{"$in":["bjsxt","admin","test"]}});

{ "_id" : ObjectId("61d93061adfe46eefd559f1f"), "name" : "bjsxt", "age" : 32 }
{ "_id" : ObjectId("61d93287adfe46eefd559f2b"), "name" : "admin", "age" : 32 }
{ "_id" : ObjectId("61db9b12d16a4f490e1c825c"), "name" : "test", "age" : 20, "gender" : "male", "hobbies" : [ "eat", "sleep" ], "phone" : 13512765432 }
> 

2查询name不包含这三个人的所有数据:

> db.users.find({"name":{"$nin":["bjsxt","admin","test"]}});

{ "_id" : ObjectId("61d8fa98aa30ebeedffca109"), "Python" : "FastAPI", "name" : "multi", "datenow" : "2022-01-08", "date" : "2022", "hobbies" : [ ] }
{ "_id" : ObjectId("61d8fafdaa30ebeedffca10b"), "Python" : "Django", "datenow" : "2022-01-08", "name" : "multi", "date" : "2022", "age" : 1 }
{ "_id" : ObjectId("61d8fafdaa30ebeedffca10c"), "name" : "multi", "datenow" : "2022-01-08", "date" : "2022", "age" : 1 }
{ "_id" : ObjectId("61d932d3adfe46eefd559f30"), "age" : 30, "username" : "xiaohong" }
>

3判断字段类型$type:

> db.users.find({"gender":{"$type":"string"}});
{ "_id" : ObjectId("61db9b12d16a4f490e1c825c"), "name" : "test", "age" : 20, "gender" : "male", "hobbies" : [ "eat", "sleep" ], "phone" : 13512765432 }
> 
> 
#查询一个字段是不是整数类型
> db.users.find({"gender":{"$type":"number"}});
#查询一个字段是不是浮点类型
> db.users.find({"gender":{"$type":"double"}});
#查询一个字段是不是整数类型
> db.users.find({"age":{"$type":"int"}});
#判断hobbies是不是数组类型
> db.users.find({"hobbies":{"$type":"array"}});
{ "_id" : ObjectId("61d8fa98aa30ebeedffca109"), "Python" : "FastAPI", "name" : "multi", "datenow" : "2022-01-08", "date" : "2022", "hobbies" : [ ] }
{ "_id" : ObjectId("61db9b12d16a4f490e1c825c"), "name" : "test", "age" : 20, "gender" : "male", "hobbies" : [ "eat", "sleep" ], "phone" : 13512765432 }
> 

4$exists判断字段值是不是null:

#判断a存在
> db.users.find({"a":{"$exists":1}});
#判断a不存在
> db.users.find({"a":{"$exists":0}});
{ "_id" : ObjectId("61d8fa98aa30ebeedffca109"), "Python" : "FastAPI", "name" : "multi", "datenow" : "2022-01-08", "date" : "2022", "hobbies" : [ ] }
{ "_id" : ObjectId("61d8fafdaa30ebeedffca10b"), "Python" : "Django", "datenow" : "2022-01-08", "name" : "multi", "date" : "2022", "age" : 1 }
{ "_id" : ObjectId("61d8fafdaa30ebeedffca10c"), "name" : "multi", "datenow" : "2022-01-08", "date" : "2022", "age" : 1 }
{ "_id" : ObjectId("61d93061adfe46eefd559f1f"), "name" : "bjsxt", "age" : 32 }
{ "_id" : ObjectId("61d93287adfe46eefd559f2b"), "name" : "admin", "age" : 32 }
{ "_id" : ObjectId("61d932d3adfe46eefd559f30"), "age" : 30, "username" : "xiaohong" }
{ "_id" : ObjectId("61db9b12d16a4f490e1c825c"), "name" : "test", "age" : 20, "gender" : "male", "hobbies" : [ "eat", "sleep" ], "phone" : 13512765432 }
> 

5正则表达式:

^开头,$结束,.任意字符,*代表0或多个,+代表一个或多个,?代表0或1个
i:不区分大小写
m:多行查找

1)查询name值以a开头的数据:

#区分大小写查找
> db.users.find({"name":{"$regex":/^a.*/}});
#不区分大小写查找以a开头的数据(简写):
> db.users.find({"name":{"$regex":/^a.*/i}});
{ "_id" : ObjectId("61d93287adfe46eefd559f2b"), "name" : "Admin", "age" : 32 }
#不区分大小写查找以a开头的数据(全写)
> db.users.find({"name":{"$regex":/^a/,"$options":"im"}});
{ "_id" : ObjectId("61d93287adfe46eefd559f2b"), "name" : "admin", "age" : 32 }
> 

 



这篇关于2022MongoDB4-find正则等其它用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程