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正则等其它用法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23MongoDB身份认证机制揭秘!
- 2024-11-20MongoDB教程:从入门到实践详解
- 2024-11-17执行 Google Ads API 查询后返回的是空数组什么原因?-icode9专业技术文章分享
- 2024-11-17google广告数据不同经理账户下的凭证可以获取对方的api数据吗?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么实现同时向多个邮箱发送邮件?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么设置header 和 标签tag 呢?-icode9专业技术文章分享
- 2024-11-12Cargo deny安装指路
- 2024-11-02MongoDB项目实战:从入门到初级应用
- 2024-11-01随时随地一键转录,Google Cloud 新模型 Chirp 2 让语音识别更上一层楼
- 2024-10-25Google Cloud动手实验详解:如何在Cloud Run上开发无服务器应用