MongoDB的查询操作

本文介绍MongoDB中的查询方法,包括find函数的使用、查询条件设置、特定类型查询及where查询等,帮助读者掌握高效的数据检索技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、Find

1.1指定需要返回的键

二、查询条件

2.1查询条件

2.2 OR 查询

2.3 $not

三、特定类型的查询

3.1 null

3.2正则表达式

3.3查询数组

3.4查询内嵌文档

四、where 查询


一、Find

>db.test.find()    批量返回集合test中的所有文档

1.1指定需要返回的键

>db.test.find({},{"username":1})      返回集合test中指定的username键(默认情况下“_id”这个键总是被返回)

db.test.find({},{'username':1})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c39"), "username" : "user1" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "username" : "user2" }

db.test.find({},{'username':1,'id':1})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c39"), "id" : 1, "username" : "user1" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2" }

如若想剔除返回结果中的某些键,可以使用:

db.test.find({},{'username':1,'_id':0})
{ "username" : "user1" }
{ "username" : "user2" }
{ "username" : "user3" }

二、查询条件

2.1查询条件

$lt ,  $lte , $gt  , $gte   分别对应  <  , <=  , >,  >= 

例如:查询 id  大于13 的用户   ,   查询 id  大于 5 小于 10 的用户

db.test.find({'id':{'$gt':13}})
{ "_id" : ObjectId("5b8e4b0006f20d3c2ec14c46"), "id" : 14, "username" : "user14", "lastUpdate" : "Tue Sep 04 2018 17:06:08 GMT+0800" }
{ "_id" : ObjectId("5b8e4b0006f20d3c2ec14c47"), "id" : 15, "username" : "user15", "lastUpdate" : "Tue Sep 04 2018 17:06:08 GMT+0800" }

db.test.find({'id':{'$gt':5,'$lt':10}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3e"), "id" : 6, "username" : "user6", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3f"), "id" : 7, "username" : "user7", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c40"), "id" : 8, "username" : "user8", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c41"), "id" : 9, "username" : "user9", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }

还有一种条件操作符 $ne ,他表示不相等;$ne  能用于多有类型的数据

例如:查询所有名字不为 user1 的用户

db.test.find({'username':{'$ne':'user1'}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3b"), "id" : 3, "username" : "user3", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }

2.2 OR 查询

MongoDB中有两种方式进行OR 查询: $in  可以用来查询一个键的多个值 ; $or  更通用一些

$in  非常灵活,可以指定不同类型的条件和值

db.test.find({'id':{'$in':[5,6,7]}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3d"), "id" : 5, "username" : "user5", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3e"), "id" : 6, "username" : "user6", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3f"), "id" : 7, "username" : "user7", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
db.test.find({'$or':[{'id':6},{'username':'user2'}]})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3e"), "id" : 6, "username" : "user6", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }

2.3 $not

 $not  是元条件句,即可以用在任何其他条件之上

拿取模运算符  $mod  来说  ,这是查询用户id 除以5 余数不等于 1 的用户 ,也就是除了 1,6,11

db.test.find({'id':{'$not':{'$mod':[5,1]}}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3b"), "id" : 3, "username" : "user3", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3c"), "id" : 4, "username" : "user4", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3d"), "id" : 5, "username" : "user5", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3f"), "id" : 7, "username" : "user7", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c40"), "id" : 8, "username" : "user8", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c41"), "id" : 9, "username" : "user9", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c42"), "id" : 10, "username" : "user10", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4b0006f20d3c2ec14c44"), "id" : 12, "username" : "user12", "lastUpdate" : "Tue Sep 04 2018 17:06:08 GMT+0800" }

三、特定类型的查询

3.1 null

null 不仅会匹配某个键值为 null  的文档,而且还会匹配不包含这个键的文档

如果仅想匹配键值为null 的文档,既要检查该键的值是否为 null ,还要通过  $exists  条件判定键值已存在

db.test.find({'age':{'$in':[null],'$exists':true}})

3.2正则表达式

正则表达式能够灵活有效地匹配字符串。如果不会可以参考 https://blog.youkuaiyun.com/YuiJar/article/details/81316478

查询user集合中 名字为 字母、数字、下划线组成的用户

db.user.find({'name':/\w+/},{'name':1})
{ "_id" : ObjectId("5b85102593bf31057845081b"), "name" : "zhaoliu" }
{ "_id" : ObjectId("5b860c8328a8c1b0ab5508c1"), "name" : "qqq" }
{ "_id" : ObjectId("5b864e958680f54603428fd2"), "name" : "wangwu" }

注意:mongodb 可以为前缀型正则表达式(比如:/^joy/)查询创建索引,所以这种类型的查询会非常高效

正则表达式也可以匹配自身,但几乎灭有人会这么做

3.3查询数组

查询数组元素与查询标量值是一样的

例如有一个水果列表  >db.food.insert({'fruit':['apple','banana','pear']})

下面的查询  >db.food.find({'fruit':'banana'})

则会成功匹配该文档

 

3.4查询内嵌文档

例如有如下文档:

{
    'name':{
        'first':'zhang',
        'last':'san'
    },
    'age':15
}

要查找姓名为 zhangsan 的人可以这样

>db.people.find({'name':{'first':'zhang','last':'san'}})

我们也可以使用点表示法查询内嵌的文档

>db.people.find({'name.first':'zhang','name:last':'san'})

四、where 查询

在非必要时,尽量避免 where 查询。where在速度上要比常规的查询慢很多,而且不安全

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值