查询内嵌文档和查询普通文档完全相同;
例如:
1
2
3
4
5
6
|
> db.post.
find
()
{
"_id"
: ObjectId(
"54ace1394ba07ed75df68f90"
),
"name"
: {
"firstname"
:
"joe"
,
"lastname"
:
"schome"
},
"age"
: 28 }
{
"_id"
: ObjectId(
"54ace14a4ba07ed75df68f91"
),
"name"
: {
"firstname"
:
"snail"
,
"lastname"
:
"yu"
},
"age"
: 29 }
{
"_id"
: ObjectId(
"54ace18d4ba07ed75df68f92"
),
"name"
: {
"firstname"
:
"sunny"
,
"lastname"
:
"wu"
},
"age"
: 26 }
> db.post.
find
({
"name"
:{
"firstname"
:
"joe"
,
"lastname"
:
"schome"
}})
{
"_id"
: ObjectId(
"54ace1394ba07ed75df68f90"
),
"name"
: {
"firstname"
:
"joe"
,
"lastname"
:
"schome"
},
"age"
: 28 } >
|
查询内嵌文档必须精确匹配,如果顺序错了也是查询不到的;
我们可以改成”.”的模式来查询内嵌的文档,这样无需精确匹配,也无关顺序;
例如:
1
2
3
|
> db.post.
find
({
"name.firstname"
:
"joe"
})
{
"_id"
: ObjectId(
"54ace1394ba07ed75df68f90"
),
"name"
: {
"firstname"
:
"joe"
,
"lastname"
:
"schome"
},
"age"
: 28 }
>
|
本文转自 bannerpei 51CTO博客,原文链接:http://blog.51cto.com/281816327/1600247,如需转载请自行联系原作者