操作符
比较操作符
当在字符串使用
gt/lt
这种比较操作符时,会查询出字符串首字母按字典排序之后/之前
的内容操作符 | query语法 | 说明 |
---|---|---|
eq | { <field> : {$eq: <value>} } | 等于 |
ne | { <field> : {$ne: <value>} } | 不等于 |
gt | { <field> : {$gt: <value>} } | 大于 |
gte | { <field> : {$gte: <value>} } | 大于等于 |
lt | { <field> : {$lt: <value>} } | 小于 |
lte | { <field> : {$lte: <value>} } | 小于等于 |
in | { <field> : {$ne: <value>} } | 在区间 |
nin | { <field> : {$ne: <value>} } | 不在区间 |
逻辑操作符
使用
and
操作符当条件都为一个字段时可以简写为{<value>: {<experssion1>,<experssion2,...>}}
当条件不为一个字段可以简写为{<value1>:<experssion1>,<value2>:<experssion2>,...}
操作符 | query语法 | 说明 |
---|---|---|
not | { <field>: { $not: {expression} } } | 匹配条件不成立的文档 |
and | { $and: [{<experssion1>,<expression2>,....}] } | 匹配条件全部成立的文档 |
or | { $or: [<experssion1>,<experssion2>,...] } | 匹配条件至少成立一条的文档 |
nor | { $nor: [<experssion1>,<experssion2>,...] } | 匹配条件全部不成立的文档 |
字段操作符
操作符 | query语法 | 说明 |
---|---|---|
exists | { <field>: { $exists: <bool> } } | 匹配字段存在的文档 |
type | { <field>: {<fieldType>} } or { <field>: [{<fieldType1>,<fieldType2>}] } | 匹配字段符合类型的文档 |
数组操作符
操作符 | query语法 | 说明 |
---|---|---|
all | { <field>: [<value>,<value>,...] } | 匹配数组字段中包含所有值的文档 |
elemMatch | { <field>: {<query1>,<query2>,...} } | 匹配字段数组里任意一个元素满足其中的条件即可 |
运算操作符
操作符 | query语法 | 说明- |
---|---|---|
$regex | { <fidld>: { $regex: /patten/ : '<options>'} } 或 { <fidld>: {/patten/ '<options>'} }(与\$in 使用的时候只能用第二种语法) | 匹配字段满足正则表达式的文档 |
查询方法
语法
查询命令为:db.<collection>.find(<query>,<projection>)
.<collection>
为要查询的集合名称<projection>
为查询的映射,相当于sql中需要查询的字段.
<query>
为查询条件其语法为{<field> : {$<op>: <value>}}
. 其中<field>
为要匹配的字段名.$<op>
为匹配符.注意在匹配符前面需要加上$
.例如,不等于就应该写作$ne
.<value>
为匹配值.
示例
查询所有文档的全部字段
# 查询所有文档的全部字段
> db.qvbilam_user.find()
# 返回如下
{ "_id" : "qvbilam_01", "name" : "errorName", "num" : 13 }
{ "_id" : ObjectId("5f39049673936f515e5c0752"), "name" : "Cc", "num" : 13 }
{ "_id" : ObjectId("5f39049673936f515e5c0753"), "name" : "Py", "num" : 13 }
{ "_id" : ObjectId("5f390671e9b6f99fc04f2b77"), "name" : "GuoTwo", "num" : 13 }
{ "_id" : ObjectId("5f390671e9b6f99fc04f2b78"), "name" : "GuoThree", "num" : 14 }
{ "_id" : ObjectId("5f39092de9b6f99fc04f2b79"), "name" : "PEPSI", "num" : 20 }
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7a"), "name" : "Coca cola", "num" : 13 }
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7b"), "name" : "gy", "num" : 12 }
{ "_id" : ObjectId("5f3918c1e9b6f99fc04f2b7e"), "name" : "save1", "num" : 1 }
{ "_id" : ObjectId("5f3918c1e9b6f99fc04f2b7f"), "name" : "save2", "num" : 2 }
{ "_id" : ObjectId("5f391930e9b6f99fc04f2b80"), "name" : "save", "num" : 12 }
查询姓名为gy的文档
# 查询姓名为gy的文档
> db.qvbilam_user.find({ name: "gy" })
# 返回如下
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7b"), "name" : "gy", "num" : 12 }
$gte
- 查询数量大于等于15的文档
# 查询数量大于等于15的文档
> db.qvbilam_user.find({ num: {$gte: 14} })
# 返回结果
{ "_id" : ObjectId("5f390671e9b6f99fc04f2b78"), "name" : "GuoThree", "num" : 14 }
{ "_id" : ObjectId("5f39092de9b6f99fc04f2b79"), "name" : "PEPSI", "num" : 20 }
$gt
- 查询姓名大于gy的文档
# 查询姓名大于gy的文档
> db.qvbilam_user.find({ name:{$gt: "gy"} })
# 结果返回 s > g的内容
{ "_id" : ObjectId("5f3918c1e9b6f99fc04f2b7e"), "name" : "save1", "num" : 1 }
{ "_id" : ObjectId("5f3918c1e9b6f99fc04f2b7f"), "name" : "save2", "num" : 2 }
{ "_id" : ObjectId("5f391930e9b6f99fc04f2b80"), "name" : "save", "num" : 12 }
$in
- 查询姓名为PEPSI或Coca cola
# 查询姓名为PEPSI或Coca cola
> db.qvbilam_user.find({ name: {$in: ["Coca cola", "PEPSI"]} })
# 返回结果如下
{ "_id" : ObjectId("5f39092de9b6f99fc04f2b79"), "name" : "PEPSI", "num" : 20 }
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7a"), "name" : "Coca cola", "num" : 13 }
$not, $in
- 查询姓名为PEPSI或Coca cola不成立的结果
# 查询姓名为PEPSI或Coca cola不成立的结果
> db.qvbilam_user.find({ name: {$not: {$in: ["Coca cola", "PEPSI"]}} })
# 返回结果如下
{ "_id" : "qvbilam_01", "name" : "errorName", "num" : 13 }
{ "_id" : ObjectId("5f39049673936f515e5c0752"), "name" : "Cc", "num" : 13 }
{ "_id" : ObjectId("5f39049673936f515e5c0753"), "name" : "Py", "num" : 13 }
{ "_id" : ObjectId("5f390671e9b6f99fc04f2b77"), "name" : "GuoTwo", "num" : 13 }
{ "_id" : ObjectId("5f390671e9b6f99fc04f2b78"), "name" : "GuoThree", "num" : 14 }
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7b"), "name" : "gy", "num" : 12 }
{ "_id" : ObjectId("5f3918c1e9b6f99fc04f2b7e"), "name" : "save1", "num" : 1 }
{ "_id" : ObjectId("5f3918c1e9b6f99fc04f2b7f"), "name" : "save2", "num" : 2 }
{ "_id" : ObjectId("5f391930e9b6f99fc04f2b80"), "name" : "save", "num" : 12 }
$and, $gt,$lte
- 查询数量大于10 并且小于等于13的文档
# 查询数量大于10 并且小于等于13的文档
> db.qvbilam_user.find({
... $and: [
... {num: {$gt: 10}},
... {num: {$lte: 13}}
... ]
... })
# 返回结果
{ "_id" : "qvbilam_01", "name" : "errorName", "num" : 13 }
{ "_id" : ObjectId("5f39049673936f515e5c0752"), "name" : "Cc", "num" : 13 }
{ "_id" : ObjectId("5f39049673936f515e5c0753"), "name" : "Py", "num" : 13 }
{ "_id" : ObjectId("5f390671e9b6f99fc04f2b77"), "name" : "GuoTwo", "num" : 13 }
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7a"), "name" : "Coca cola", "num" : 13 }
{ "_id" : ObjectId("5f390b1ee9b6f99fc04f2b7b"), "name" : "gy", "num" : 12 }
{ "_id" : ObjectId("5f391930e9b6f99fc04f2b80"), "name" : "save", "num" : 12 }
$and
- 简写
# 字段相同的情况下and的简写
> db.qvbilam_user.find({
... num: {$gt:10, $lte:13}
... })
# 字段不通情况下and的简写
> db.qvbilam_user.find({
... name: {$eq: "gy"},
... num: {$lte: 13}
... })
$exists
- 查询存在num字段的文档
# 查询存在num字段的文档
> db.qvbilam_user.find({ num: {$exists: true} })
$type
- 查询_id字段类型为string的文档
# 查询_id字段类型为string的文档
> db.qvbilam_user.find({ _id: {$type: "string"} })
# 返回结果
{ "_id" : "qvbilam_01", "name" : "errorName", "num" : 13 }
- 查询_id字段类型为string和objectId的文档
# 查询_id字段类型为string和ObjectId的文档
db.qvbilam_user.find({ _id: {$type: ["string","objectId"]} })
$all
- 查询满足所有数组字段的文档
# 创建用户地址数组
> db.qvbilam_user.insert([
... {
... name: "MoFan",
... num: 13,
... location: ["China","Hebei","ShiJiaZhuang"],
... mobile: 13211111111
... },
... {
... name: "JunJie",
... num: 14,
... location: ["China","ZheJiang","HangZhou"],
... mobile: 13222222222
... },
... {
... name: "LvKe",
... num: 15,
... location: ["China","BeiJing","ChaoYang"],
... mobile: [13233333333,13244444444,13255555555]
... }
... ])
# 通过$all查询满足在中国,浙江,杭州的文档
> db.qvbilam_user.find({
... location: { $all: ["China","ZheJiang","HangZhou"] }
... })
# 返回如下
{ "_id" : ObjectId("5f3d31f16dbe86c585d88d1a"), "name" : "JunJie", "num" : 14, "location" : [ "China", "ZheJiang", "HangZhou" ], "mobile" : 13222222222 }
# 通过$all查询手机号是13233333333,13244444444,13255555555
> db.qvbilam_user.find({
... mobile: { $all: [[13233333333, 13244444444,13255555555]] }
... })
# 返回
{ "_id" : ObjectId("5f3d31f16dbe86c585d88d1b"), "name" : "LvKe", "num" : 15, "location" : [ "China", "BeiJing", "ChaoYang" ], "mobile" : [ 13233333333, 13244444444, 13255555555 ] }
$elemMatch
- 匹配数组字段至少满足一个值的文档
# 查询手机号在13220000000,13240000000的文档
> db.qvbilam_user.find({
... mobile: { $elemMatch: { $gt:13211111111, $lte:13240000000} }
... })
# 返回如下
{ "_id" : ObjectId("5f3d31f16dbe86c585d88d1b"), "name" : "LvKe", "num" : 15, "location" : [ "China", "BeiJing", "ChaoYang" ], "mobile" : [ 13233333333, 13244444444, 13255555555 ] }
$all, $elemMatch
- 满足多个条件的数组
# 查询条件为手机号在13210000000~1322000000之间 同时另一个元素满足 132400000000~13250000000之间的文档
> db.qvbilam_user.find({
... mobile:{ $all: [
... {$elemMatch: {$gt: 13230000000,$lt:13240000000}},
... {$elemMatch: {$gt: 13240000000,$lt:13260000000}}
... ] }
... })
# 返回如下
{ "_id" : ObjectId("5f3d31f16dbe86c585d88d1b"), "name" : "LvKe", "num" : 15, "location" : [ "China", "BeiJing", "ChaoYang" ], "mobile" : [ 13233333333, 13244444444, 13255555555 ] }
$regex
第一种写法
# 查询名称包含guo且不区分大小写的文档
db.qvbilam.find({ name: {$regex: /guo/, $options: "i"} })
# 返回
{ "_id" : ObjectId("602f55e24efaa03816f5dbfd"), "name" : "GuoOne", "num" : 15 }
{ "_id" : ObjectId("602f55e24efaa03816f5dbfe"), "name" : "GuoTwo", "num" : 20 }
{ "_id" : ObjectId("602f561e4efaa03816f5dbff"), "name" : "GuoThree", "num" : 21 }
{ "_id" : ObjectId("602f561e4efaa03816f5dc00"), "name" : "GuoFour", "num" : 22 }
{ "_id" : ObjectId("602f561e4efaa03816f5dc01"), "name" : "GuoFive", "num" : 25 }
第二种写法
# 查询名称以 a 或 q开偷的文档
db.qvbilam.find({ name: { $in: [/^a/,/^q/] } })
# 返回如下
{ "_id" : "qvbilam_01", "name" : "angelQvbilam", "num" : 10 }
{ "_id" : ObjectId("602f55994efaa03816f5dbfc"), "name" : "qvbilam", "num" : 10 }
映射
默认查询结果返回所有字段,而映射文档限制了查询文档返回的字段
映射运算符
操作符 | 说明 |
---|---|
$ | 投影数组筛选条件与find命令中的筛选条件相同可以使用"field.$" : true,(引号是必须的) .来替代$elemMatch |
$elemMatch | 遍历数组字段中的每一个元素,找到符合匹配条件的数组字段,并返回数组字段中的满足条件的第一个元素 |
$slice | 限制从阵列映射的元素数。支持跳过和限制切片 |
语法
db.qvbilam.find(<>,),projection为显示或者不显示的字段,值的格式为 : , : .value的值代表显示或者不显示,可以是1, 0也可以是true, false. 也可一
- 默认情况显示_id,字段
- value = 1 或 true 时,字段显示
- value = 0 或 false 时,字段不显示
- value 可以是映射运算符
映射的字段要么全显示要么全不显示,不能混合使用. _id除外,因为_id为特殊字段
示例
# 不使用映射查询的结果,方便和下面的示例进行对比
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
})
# 返回如下
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "num" : 21, "location" : [ "HeBei", "ShiJiaZhuang", "ShenZe" ] }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "num" : 50, "location" : [ "ZheJiang", "HangZhou", "XiaoShan" ] }
显示字段
# 只显示name,location 字段
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ name: true, location: true })
# 返回
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "num" : 21, "location" : [ "HeBei", "ShiJiaZhuang", "ShenZe" ] }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "num" : 50, "location" : [ "ZheJiang", "HangZhou", "XiaoShan" ] }
不显示字段
# 不显示_id,name字段
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ _id: false, name: false })
# 返回
{ "num" : 21, "location" : [ "HeBei", "ShiJiaZhuang", "ShenZe" ] }
{ "num" : 50, "location" : [ "ZheJiang", "HangZhou", "XiaoShan" ] }
显示混合使用
当混合使用的字段为_id
# 不显示_id,显示name
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ _id: false, name: true })
# 返回
{ "name" : "PiaoYao" }
{ "name" : "ZhaoX" }
当混合使用的字段不为_id
# 不显示num, 显示name
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ num: false, name: true })
# 返回错误
Error: error: {
"ok" : 0,
"errmsg" : "Cannot do inclusion on field name in exclusion projection",
"code" : 31253,
"codeName" : "Location31253"
}
$
# 查询locaiton大于等于Z,投影条件与查找的筛选条件相同时
db.qvbilam.find({
location: { $gte: "Z" }
},{ _id: false, name: true, "location.$": true })
# 返回
{ "name" : "XianXian", "location" : "河北" }
{ "name" : "ZhaoX", "location" : [ "ZheJiang" ] }
$elemMatch
# 遍历每个location数组里的元素有其中一条 > HangZhou的值,并返回该数组字段满足条件的第一个值
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ name: true, location: { $elemMatch: {$gt: "HangZhou"} } })
# 返回
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "location" : [ "HeBei" ] }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "location" : [ "ZheJiang" ] }
返回结果说明
- 第一个返回值数组为
[ "HeBei", "ShiJiaZhuang", "ShenZe" ]
- 第一个返回值按字母排序
HeBei > HangZhou
所以显示 - 第二个返回值数组为
[ "ZheJiang", "HangZhou", "XiaoShan" ]
- 第二个返回值按字母排序
ZheJiang > HangZhou
,所以显示
$slice
#----------------------------------------------------
# 返回location数组字段中的第一个元素
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ name: true, location: { $slice: 1 } })
# 返回
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "location" : [ "HeBei" ] }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "location" : [ "ZheJiang" ] }
#----------------------------------------------------
# 返回location数组字段中的最后一个元素
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ name: true, location: { $slice: -1 } })
# 返回
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "location" : [ "ShenZe" ] }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "location" : [ "XiaoShan" ] }
#----------------------------------------------------
# 返回location数组字段中的第一个元素和第三个元素
db.qvbilam.find({
$or: [ {name: "PiaoYao"}, {name: "ZhaoX"}]
},{ name: true, location: { $slice: [1, 3] } })
# 返回
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "location" : [ "ShiJiaZhuang", "ShenZe" ] }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "location" : [ "HangZhou", "XiaoShan" ] }
游标
说明
db.collection.find()
返回的是一个游标结果,访问文档需要遍历这个游标值。当遍历完游标后或者没遍历完游标10分钟后会自动关闭游标.
可以使用
var Cursor = db.collection.find().noCursorTimeout()
防止未遍历完的游标在10分钟后自动关闭.可以使用Cursor.close()
主动关闭 游标方法
组合使用游标方法的顺序
sort > skip > limit
方法 | 示例 | 说明 |
---|---|---|
count | myCursor.count(bool) | 对文档计数,当bool为true时不回考虑limit进行计数.默认为true |
close() | myCursor.clos() | 关闭游标 |
forEach() | myCursor.forEach(function) | 遍历游标funtion为遍历输出的样式 |
hasNext() | myCursor.hasNext | 是否还有下一个游标 |
next() | myCursor.next() | 下个游标 |
limit() | myCursor.limit(n) | 显示游标数量 |
skip() | myCursor.skip(n) | 跳过游标数量 |
sort | myCursor.sort({<field1>:<order>,<field2>:<order>,...}) | 对文档进行排序.order为1正序 |
示例
声明游标
使用var
# 生命user为查询的结果
var user = db.qvbilam.find({ num: {$gt: 30} })
# 没有自动遍历文档
>
# 调用游标
user
# 返回
{ "_id" : ObjectId("602f563e4efaa03816f5dc02"), "name" : "HelloOne", "num" : 31 }
{ "_id" : ObjectId("602f563e4efaa03816f5dc03"), "name" : "HelloTwo", "num" : 32 }
{ "_id" : ObjectId("602f563e4efaa03816f5dc04"), "name" : "HelloThree", "num" : 35 }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "num" : 50, "location" : [ "ZheJiang", "HangZhou", "XiaoShan" ] }
# 再次调用游标
user
# 返回空
>
不使用var
user = db.qvbilam.find({ num: {$gt: 30} })
# 直接返回结果
{ "_id" : ObjectId("602f563e4efaa03816f5dc02"), "name" : "HelloOne", "num" : 31 }
{ "_id" : ObjectId("602f563e4efaa03816f5dc03"), "name" : "HelloTwo", "num" : 32 }
{ "_id" : ObjectId("602f563e4efaa03816f5dc04"), "name" : "HelloThree", "num" : 35 }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "num" : 50, "location" : [ "ZheJiang", "HangZhou", "XiaoShan" ] }
# 再次调用
user
# 无结果返回
>
$forEach
# 遍历以json格式输出
db.qvbilam.find().forEach(printjson)
# 返回部分结果
{ "_id" : "qvbilam_01", "name" : "angelQvbilam", "num" : 10 }
{
"_id" : ObjectId("602f55994efaa03816f5dbfc"),
"name" : "qvbilam",
"num" : 10
}
$skip
# 获取文档数量
db.qvbilam.count()
# 返回文档数量
13
# 获取跳过前10条文档的结果(显示后13 - 10条)
db.qvbilam.find().skip(10)
# 返回
{ "_id" : ObjectId("602f5866c4f596e5b431db1c"), "name" : "XianXian", "num" : 21, "location" : "河北" }
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "num" : 21, "location" : [ "HeBei", "ShiJiaZhuang", "ShenZe" ] }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "num" : 50, "location" : [ "ZheJiang", "HangZhou", "XiaoShan" ] }
$sort
# 对num进行排序
db.qvbilam.find().sort({ num: 1 })
# 返回
{ "_id" : "qvbilam_01", "name" : "angelQvbilam", "num" : 10 }
{ "_id" : ObjectId("602f55994efaa03816f5dbfc"), "name" : "qvbilam", "num" : 10 }
{ "_id" : ObjectId("602f55e24efaa03816f5dbfd"), "name" : "GuoOne", "num" : 15 }
{ "_id" : ObjectId("602f55e24efaa03816f5dbfe"), "name" : "GuoTwo", "num" : 20 }
{ "_id" : ObjectId("602f561e4efaa03816f5dbff"), "name" : "GuoThree", "num" : 21 }
{ "_id" : ObjectId("602f5866c4f596e5b431db1c"), "name" : "XianXian", "num" : 21, "location" : "河北" }
{ "_id" : ObjectId("602f7e8dc4f596e5b431db1d"), "name" : "PiaoYao", "num" : 21, "location" : [ "HeBei", "ShiJiaZhuang", "ShenZe" ] }
{ "_id" : ObjectId("602f561e4efaa03816f5dc00"), "name" : "GuoFour", "num" : 22 }
{ "_id" : ObjectId("602f561e4efaa03816f5dc01"), "name" : "GuoFive", "num" : 25 }
{ "_id" : ObjectId("602f563e4efaa03816f5dc02"), "name" : "HelloOne", "num" : 31 }
{ "_id" : ObjectId("602f563e4efaa03816f5dc03"), "name" : "HelloTwo", "num" : 32 }
{ "_id" : ObjectId("602f563e4efaa03816f5dc04"), "name" : "HelloThree", "num" : 35 }
{ "_id" : ObjectId("602f7eccc4f596e5b431db1e"), "name" : "ZhaoX", "num" : 50, "location" : [ "ZheJiang", "HangZhou", "XiaoShan" ] }