有两种方式可以查询内嵌文档
查询整个文档
针对键值对进行查询
查询整个文档
db.emp.insert({
"id":"A001",
"name":{
"first":"Carey",
"last":"Ickes"
},
"age":25
})
db.emp.find({"name" : { "first" : "Carey", "last" : "Ickes" }})
> db.emp.find()
{ "_id" : ObjectId("561720d7de256e5153080ea7"), "id" : "A001", "name" : { "first" : "Carey", "last" : "Ickes" }, "age" : 25 }
> bb = db.emp.findOne({ "_id" : ObjectId("561720d7de256e5153080ea7") })
{
"_id" : ObjectId("561720d7de256e5153080ea7"),
"id" : "A001",
"name" : {
"first" : "Carey",
"last" : "Ickes"
},
"age" : 25
}
> db.emp.find({"name" : { "first" : "Carey", "last" : "Ickes" }})
{ "_id" : ObjectId("561720d7de256e5153080ea7"), "id" : "A001", "name" : { "first" : "Carey", "last" : "Ickes" }, "age" : 25 }
> bb.name = { "first" : "Carey", "last" : "Ickes", "middle" : "Joe" }
{ "first" : "Carey", "last" : "Ickes", "middle" : "Joe" }
> db.emp.save(bb)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.emp.find({"name" : { "first" : "Carey", "last" : "Ickes" }})
这种查询会去精确匹配整个内嵌文档。如果其中name元素的子集发生变化,查询条件不与整个内嵌文档相匹配。
而且这种查询,还与顺序有关系。
键值对查询
查询文档时,可以用”.”来表示进入内嵌文档
> db.emp.find({ "name.last" : "Ickes", "name.first" : "Carey" })
{ "_id" : ObjectId("561720d7de256e5153080ea7"), "id" : "A001", "name" : { "first" : "Carey", "last" : "Ickes", "middle" : "Joe" }, "age" : 25 }
现在,如果Ickes增加了更多的键,这个查询依然会匹配他的姓跟名。
数组里包含内嵌文档的查询
db.blog.insert({
"_id":"B001",
"title":"MongoDB查询",
"comments":[
{
"name":"ickes",
"score":3,
"comment":"nice"
},
{
"name":"xl",
"score":4,
"comment":"nice"
},
{
"name":"eksliang",
"score":5,
"comment":"nice"
},
{
"name":"ickes",
"score":6,
"comment":"nice"
}
]
})
db.blog.insert({
"_id":"B002",
"title":"MySQL查询",
"comments":[
{
"name":"MySQL-ickes",
"score":4,
"comment":"nice"
},
{
"name":"MySQL-xl",
"score":9,
"comment":"nice"
},
{
"name":"MySQL-eksliang",
"score":5,
"comment":"nice"
},
{
"name":"MySQL-ickes",
"score":6,
"comment":"nice"
}
]
})
# 以下两种查询都是精确匹配文档,所以查询不出结果
> db.blog.find({ "comments" : { "name": "ickes", "score" : { "$gt" : 5 } } })
> db.blog.find({ "comments" : { "name": "ickes", "score" : { "$gt" : 5 }, "comment":"nice" } })
#
查询条件里面的键值对会解释为AND,但是对于数组的内嵌文档来说它会解释为OR的关系。也就是
说上面的解释会拆解为 comments.name : ickes 或者 comments.score : { "$gt" : 5 }
> db.blog.find({ "comments.name" : "ickes", "comments.score" : { "$gt":5 } })
{ "_id" : "B001", "title" : "MongoDB查询", "comments" : [ { "name" : "ickes", "score" : 3, "comment" : "nice" }, { "name" : "xl", "score" : 4, "comment" : "nice" }, { "name" : "eksliang", "score" : 5, "comment" : "nice" }, { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
查询 “name” : “ickes”, “score” : { “$gt” : 5 }
> db.blog.find({"comments" : { "$elemMatch" : { "name" : "ickes", "score" : { "$gt" : 5 } } }})
{ "_id" : "B001", "title" : "MongoDB查询", "comments" : [ { "name" : "ickes", "score" : 3, "comment" : "nice" }, { "name" : "xl", "score" : 4, "comment" : "nice" }, { "name" : "eksliang", "score" : 5, "comment" : "nice" }, { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
查询 “score” : { “$gt” : 5 }
> db.blog.find({"comments" : { "$elemMatch" : { "score" : { "$gt" : 5 } } }})
{ "_id" : "B001", "title" : "MongoDB查询", "comments" : [ { "name" : "ickes", "score" : 3, "comment" : "nice" }, { "name" : "xl", "score" : 4, "comment" : "nice" }, { "name" : "eksliang", "score" : 5, "comment" : "nice" }, { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
{ "_id" : "B002", "title" : "MySQL查询", "comments" : [ { "name" : "MySQL-ickes", "score" : 4, "comment" : "nice" }, { "name" : "MySQL-xl", "score" : 9, "comment" : "nice" }, { "name" : "MySQL-eksliang", "score" : 5, "comment" : "nice" }, { "name" : "MySQL-ickes", "score" : 6, "comment" : "nice" } ] }
返回与查询条件相匹配的任意一个数组元素
> db.blog.find({
"comments" : {
"$elemMatch" : {
"name" : "ickes",
"score" : { "$gt" : 5 }
}
}
},
{
"comments.$" : 2
}
)
> { "_id" : "B001", "comments" : [ { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
> qq = db.blog.findOne({ "_id" : "B002" })
> qq.comments = [
{
"name" : "ickes",
"score" : 9,
"comment" : "nice"
},
{
"name" : "MySQL-xl",
"score" : 9,
"comment" : "nice"
},
{
"name" : "MySQL-eksliang",
"score" : 5,
"comment" : "nice"
},
{
"name" : "MySQL-ickes",
"score" : 6,
"comment" : "nice"
}
]
> db.blog.save(qq)
再次执行
db.blog.find({
"comments" : {
"$elemMatch" : {
"name" : "ickes",
"score" : { "$gt" : 5 }
}
}
},
{
"comments.$" : 2
}
)
> db.blog.find({ "comments" : { "$elemMatch" : { "name" : "ickes", "score" : { "$gt" : 5 } } } }, { "comments.$" : 2 })
{ "_id" : "B001", "comments" : [ { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
{ "_id" : "B002", "comments" : [ { "name" : "ickes", "score" : 9, "comment" : "nice" } ] }
>
最后再执行
db.blog.find({
"comments" : {
"$elemMatch" : {
"name" : "ickes",
"score" : { "$gt" : 5 }
}
}
},
{
"comments.$" : 1
}
)
{ "_id" : "B001", "comments" : [ { "name" : "ickes", "score" : 6, "comment" : "nice" } ] }
{ "_id" : "B002", "comments" : [ { "name" : "ickes", "score" : 9, "comment" : "nice" } ] }
注意只有一个查询条件,就可以不使用$elemMatch
下面两个查询是等价的
db.blog.find({ "comments.score" : { "$gt" : 5 } })
db.blog.find({ "comments" : { "$elemMatch" : { "score" : { "$gt" : 6 } } } })
$elemMatch 的用法
- 元素匹配
db.nums.insert([{ "results" : [ 82, 85, 88 ] },{ "results" : [ 75, 88, 89 ] }])
查询 80 <= x < 85 的数据,数组中的任何一个元素都要
大于等于80并且小于等于85
> db.nums.find({ "results" : { "$elemMatch" : { "$gte" : 80, "$lt" : 85 } } })
{ "_id" : ObjectId("5617320ade256e5153080ea9"), "results" : [ 82, 85, 88 ] }
- 数组内嵌文档
db.survey.insert([
{ _id: 1, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] },
{ _id: 2, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] },
{ _id: 3, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 } ] }
{ _id: 4, results: [ { product: "abc", score: 10 }, { product: "xyz", score: 5 } ] },
{ _id: 5, results: [ { product: "abc", score: 8 }, { product: "xyz", score: 7 } ] },
{ _id: 6, results: [ { product: "abc", score: 7 }, { product: "xyz", score: 8 } ] }
])
查询 product 为 xyz,并且score 大于 8 的集合
> db.survey.find({ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } })
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }
{ "_id" : 6, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }
> db.survey.find({ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }, { "results.$" : 1 })
{ "_id" : 3, "results" : [ { "product" : "xyz", "score" : 8 } ] }
{ "_id" : 6, "results" : [ { "product" : "xyz", "score" : 8 } ] }
- 单一查询条件
如果只有单一的查询条件,那么可以不用 $elemMatch
db.survey.find({
results: { $elemMatch: { product: "xyz" } }
})
等同于
db.survey.find(
{ "results.product": "xyz" }
)