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-12-24MongoDB资料:新手入门完全指南
- 2024-12-20go-zero 框架的 RPC 服务 启动start和停止 底层是怎么实现的?-icode9专业技术文章分享
- 2024-12-19Go-Zero 框架的 RPC 服务启动和停止的基本机制和过程是怎么实现的?-icode9专业技术文章分享
- 2024-12-18怎么在golang中使用gRPC测试mock数据?-icode9专业技术文章分享
- 2024-12-15掌握PageRank算法核心!你离Google优化高手只差一步!
- 2024-12-15GORM 中的标签 gorm:"index"是什么?-icode9专业技术文章分享
- 2024-12-11怎么在 Go 语言中获取 Open vSwitch (OVS) 的桥接信息(Bridge)?-icode9专业技术文章分享
- 2024-12-11怎么用Go 语言的库来与 Open vSwitch 进行交互?-icode9专业技术文章分享
- 2024-12-11怎么在 go-zero 项目中发送阿里云短信?-icode9专业技术文章分享
- 2024-12-11怎么使用阿里云 Go SDK (alibaba-cloud-sdk-go) 发送短信?-icode9专业技术文章分享