mongodb各种查询示例(全)

 查询操作

集合查询方法find()

有testUsers集合中有以下数据:

> db.testUsers.find({})
{ "_id" : ObjectId("5c09d691c2ed68e91956aab0"), "name" : "zhangsan", "age" : 16, "sex" : "male" }
{ "_id" : ObjectId("5c09d69dc2ed68e91956aab1"), "name" : "wangwu", "age" : 18, "sex" : "male" }
{ "_id" : ObjectId("5c09d6b0c2ed68e91956aab2"), "name" : "xiaohong", "age" : 28, "sex" : "female" }
{ "_id" : ObjectId("5c09d6bac2ed68e91956aab3"), "name" : "xiaoming", "age" : 28, "sex" : "male" }

查找所有文档

//将返回集合中所有文档
db.collection.find()
//或者
db.collection.find({})

精准查找

// 查找age=28的文档
> db.testUsers.find({'age':28})
{ "_id" : ObjectId("5c09d6b0c2ed68e91956aab2"), "name" : "xiaohong", "age" : 28, "sex" : "female" }
{ "_id" : ObjectId("5c09d6bac2ed68e91956aab3"), "name" : "xiaoming", "age" : 28, "sex" : "male" }

精准组合查找

// 查找age=28,且sex=male的文档
> db.testUsers.find({'age':28, 'sex':'male'})
{ "_id" : ObjectId("5c09d6bac2ed68e91956aab3"), "name" : "xiaoming", "age" : 28, "sex" : "male" }

指定返回的字段

// 全量查询,只返回name字段
> db.testUsers.find({},{'name':1})
{ "_id" : ObjectId("5c09d691c2ed68e91956aab0"), "name" : "zhangsan" }
{ "_id" : ObjectId("5c09d69dc2ed68e91956aab1"), "name" : "wangwu" }
{ "_id" : ObjectId("5c09d6b0c2ed68e91956aab2"), "name" : "xiaohong" }
{ "_id" : ObjectId("5c09d6bac2ed68e91956aab3"), "name" : "xiaoming" }

// 返回结果中移除_id字段
> db.testUsers.find({},{'name':1,'_id':0})
{ "name" : "zhangsan" }
{ "name" : "wangwu" }
{ "name" : "xiaohong" }
{ "name" : "xiaoming" }

// 返回age=28的所有name
> db.testUsers.find({'age':28},{'name':1,'age':1,'_id':0})
{ "name" : "xiaohong", "age" : 28 }
{ "name" : "xiaoming", "age" : 28 }

 

查询内嵌文档

embeddedUsers集合有以下数据

> db.embeddedUsers.find()
{ "_id" : ObjectId("5c09db06c2ed68e91956aab4"), "enName" : { "first" : "zhang", "last" : "san" }, "age" : 18, "hobby" : [ { "hName" : "basketball", "duration" : "2 years" }, { "hName" : "swimming", "duration" : "1 years" } ], "favoriteFruit" : [ "banana", "apple" ] }
{ "_id" : ObjectId("5c09db5bc2ed68e91956aab5"), "enName" : { "first" : "wang", "last" : "wu" }, "age" : 20, "hobby" : [ { "hName" : "basketball", "duration" : "2 years" }, { "hName" : "football", "duration" : "3 years" } ], "favoriteFruit" : [ "grape", "apple" ] }
{ "_id" : ObjectId("5c09dd77c2ed68e91956aab6"), "enName" : { "first" : "zhang", "last" : "yang" }, "age" : 28, "hobby" : [ { "hName" : "pingpang", "duration" : "5 years" }, { "hName" : "swimming", "duration" : "1 years" } ], "favoriteFruit" : [ "banana", "orange" ] }
 

数组查询

// 查找喜欢grape水果的人
> db.embeddedUsers.find({'favoriteFruit':'grape'}, {'enName':1, 'favoriteFruit':1, '_id':0})
{ "enName" : { "first" : "wang", "last" : "wu" }, "favoriteFruit" : [ "grape", "apple" ] }

嵌套查询

使用"."号来表示嵌套关系

// 查询first叫=zhang的人
> db.embeddedUsers.find({'enName.first':'zhang'},{'enName':1, '_id':0})
{ "enName" : { "first" : "zhang", "last" : "san" } }
{ "enName" : { "first" : "zhang", "last" : "yang" } }

// 查询first叫=zhang 且 hobby=basketball的人
> db.embeddedUsers.find({'enName.first':'zhang', 'hobby.hName':'basketball'},{'enName':1, '_id':0})
{ "enName" : { "first" : "zhang", "last" : "san" } }

// 查询喜欢吃banana 且 age>=20的人
> db.embeddedUsers.find({'favoriteFruit':'banana', 'age':{'$gte':20}})
{ "_id" : ObjectId("5c09dd77c2ed68e91956aab6"), "enName" : { "first" : "zhang", "last" : "yang" }, "age" : 28, "hobby" : [ { "hName" : "pingpang", "duration" : "5 years" }, { "hName" : "swimming", "duration" : "1 years" } ], "favoriteFruit" : [ "banana", "orange" ] }

使用$elemMatch操作符来表示嵌套关系

// 查询业余爱好为basketball的人
> db.embeddedUsers.find({'hobby':{'$elemMatch':{'hName':'basketball'}}})
{ "_id" : ObjectId("5c09db06c2ed68e91956aab4"), "enName" : { "first" : "zhang", "last" : "san" }, "age" : 18, "hobby" : [ { "hName" : "basketball", "duration" : "2 years" }, { "hName" : "swimming", "duration" : "1 years" } ], "favoriteFruit" : [ "banana", "apple" ] }
{ "_id" : ObjectId("5c09db5bc2ed68e91956aab5"), "enName" : { "first" : "wang", "last" : "wu" }, "age" : 20, "hobby" : [ { "hName" : "basketball", "duration" : "2 years" }, { "hName" : "football", "duration" : "3 years" } ], "favoriteFruit" : [ "grape", "apple" ] }

// 查询业余爱好为basketball 且 持续时间为2 years以上的人
> db.embeddedUsers.find({'hobby':{'$elemMatch':{'hName':'basketball', 'duration':{'$gte':'2 years'}}}})
{ "_id" : ObjectId("5c09db06c2ed68e91956aab4"), "enName" : { "first" : "zhang", "last" : "san" }, "age" : 18, "hobby" : [ { "hName" : "basketball", "duration" : "2 years" }, { "hName" : "swimming", "duration" : "1 years" } ], "favoriteFruit" : [ "banana", "apple" ] }
{ "_id" : ObjectId("5c09db5bc2ed68e91956aab5"), "enName" : { "first" : "wang", "last" : "wu" }, "age" : 20, "hobby" : [ { "hName" : "basketball", "duration" : "2 years" }, { "hName" : "football", "duration" : "3 years" } ], "favoriteFruit" : [ "grape", "apple" ] }
// 查询业余爱好为basketball 且 持续时间为3 years以上的人
> db.embeddedUsers.find({'hobby':{'$elemMatch':{'hName':'basketball', 'duration':{'$gte':'3 years'}}}})
>

查询操作符

下面我们将配合查询操作符来执行复杂的查询操作,比如元素查询、 逻辑查询 、比较查询操作。 

'$gt', '$gte', '$lt', '$ne'等比较操作符

使用下面的比较操作符"$gt" 、"$gte"、 "$lt"、 "$lte"(分别对应">"、 ">=" 、"<" 、"<="),组合起来进行范围的查找。

// 查询testUsers中年龄在[18,28)之间的人
> db.testUsers.find({'age':{'$gte':18,'$lt':28}})
{ "_id" : ObjectId("5c09d69dc2ed68e91956aab1"), "name" : "wangwu", "age" : 18, "sex" : "male" }

// 查询年龄不等于28的人
> db.testUsers.find({'age':{'$ne':28}})
{ "_id" : ObjectId("5c09d691c2ed68e91956aab0"), "name" : "zhangsan", "age" : 16, "sex" : "male" }
{ "_id" : ObjectId("5c09d69dc2ed68e91956aab1"), "name" : "wangwu", "age" : 18, "sex" : "male" }

键值为null的查询

插入一条sex为null的数据

> db.testUsers.insert({"name" : "wanggang", "age" : 30, "sex" : null })

find({sex:{in:[null],in:[null],exists:true }})表达式

// 查询sex=null的人
> db.testUsers.find({'sex':{'$in':[null]}})
{ "_id" : ObjectId("5c09e30ec2ed68e91956aab7"), "name" : "wanggang", "age" : 30, "sex" : null }

// $exists判定集合中文档是否包含该键
> db.testUsers.find({'sex':{'$in':[null], '$exists':true}})
{ "_id" : ObjectId("5c09e30ec2ed68e91956aab7"), "name" : "wanggang", "age" : 30, "sex" : null }
> db.testUsers.find({'sex':{'$in':[null], '$exists':false}})

直接查询键值为null的也可以

> db.testUsers.find({'sex':null})
{ "_id" : ObjectId("5c09e30ec2ed68e91956aab7"), "name" : "wanggang", "age" : 30, "sex" : null }

但是这里的null不仅仅匹配自身,还匹配“不存在的”文档,集合中文档都不存在"xxx"键的都会匹配查询条件

> db.testUsers.find({'xxx':null})
{ "_id" : ObjectId("5c09d691c2ed68e91956aab0"), "name" : "zhangsan", "age" : 16, "sex" : "male" }
{ "_id" : ObjectId("5c09d69dc2ed68e91956aab1"), "name" : "wangwu", "age" : 18, "sex" : "male" }
{ "_id" : ObjectId("5c09d6b0c2ed68e91956aab2"), "name" : "xiaohong", "age" : 28, "sex" : "female" }
{ "_id" : ObjectId("5c09d6bac2ed68e91956aab3"), "name" : "xiaoming", "age" : 28, "sex" : "male" }
{ "_id" : ObjectId("5c09e30ec2ed68e91956aab7"), "name" : "wanggang", "age" : 30, "sex" : null }

'$all'

// 查询喜欢的水果有banana的文档
> db.embeddedUsers.find({'favoriteFruit':{'$all':['banana']}}, {'favoriteFruit':1, '_id':0})
{ "favoriteFruit" : [ "banana", "apple" ] }
{ "favoriteFruit" : [ "banana", "orange" ] }

// 查询喜欢的水果有banana,orange的文档,"banana,orange"排序不分先后
> db.embeddedUsers.find({'favoriteFruit':{'$all':['banana','orange']}}, {'favoriteFruit':1, '_id':0})
{ "favoriteFruit" : [ "banana", "orange" ] }

// 查询喜欢的水果有banana,orange,apple的文档
> db.embeddedUsers.find({'favoriteFruit':{'$all':['banana','orange','apple']}}, {'favoriteFruit':1, '_id':0})
> 

查询数组中指定位置的元素

> db.embeddedUsers.find({'favoriteFruit.0':'orange'}, {'favoriteFruit':1})
> db.embeddedUsers.find({'favoriteFruit.1':'orange'}, {'favoriteFruit':1})
{ "_id" : ObjectId("5c09dd77c2ed68e91956aab6"), "favoriteFruit" : [ "banana", "orange" ] }

'$size' 查询指定长度的数组

// 查询favoriteFruit数组长度为2的文档
> db.embeddedUsers.find({'favoriteFruit':{'$size':2}},{'favoriteFruit':1})
{ "_id" : ObjectId("5c09db06c2ed68e91956aab4"), "favoriteFruit" : [ "banana", "apple" ] }
{ "_id" : ObjectId("5c09db5bc2ed68e91956aab5"), "favoriteFruit" : [ "grape", "apple" ] }
{ "_id" : ObjectId("5c09dd77c2ed68e91956aab6"), "favoriteFruit" : [ "banana", "orange" ] }

// 查询favoriteFruit数组长度为5的文档
> db.embeddedUsers.find({'favoriteFruit':{'$size':5}},{'favoriteFruit':1})
> 

注意:size必须制定一个定值,不能接受一个范围值,不能与其他查询子句组合(比如"size必须制定一个定值,不能接受一个范围值,不能与其他查询子句组合(比如"gt")。

'$in' 匹配键值等于指定数组中任意值的文档

// 查询年龄有10,30的文档
> db.embeddedUsers.find({'age':{'$in':[10,30]}}, {'age':1})
> 

// 查询年龄有10,20的文档
> db.embeddedUsers.find({'age':{'$in':[10,20]}}, {'age':1})
{ "_id" : ObjectId("5c09db5bc2ed68e91956aab5"), "age" : 20 }

'$and' 

语法: { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
// 查询姓氏为zhang 且 年龄为18的文档
> db.embeddedUsers.find({'$and':[{'enName.first':'zhang'}, {'age':18}]})
{ "_id" : ObjectId("5c09db06c2ed68e91956aab4"), "enName" : { "first" : "zhang", "last" : "san" }, "age" : 18, "hobby" : [ { "hName" : "basketball", "duration" : "2 years" }, { "hName" : "swimming", "duration" : "1 years" } ], "favoriteFruit" : [ "banana", "apple" ] }

等同于

> db.embeddedUsers.find({'enName.first':'zhang','age':18})

其他

 

参考:

MongoDB查询;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值