Reputation: 109
Hi i have a data model like this
module{
name: "xx",
sa: [
{
sa_name: "yy",
fact: [
fact_name: "zz"
],
dim: [
dim_name: "qq"
]
}
]
}
I have sa embedded in module and fact and dim embedded inside sa.
I tried db.coll.find({"module.sa.fact.name":"zz"},{})
is not working where as for single nest db.coll.find({"module.sa.name":"yy"},{})
is working fine.
How can i query this sub document inside sub document.
Upvotes: 2
Views: 7527
Reputation: 534
For me the query is just working fine, see mongo 2.4.8 shell:
> db.coll.insert({module: {name:"xx", sa: [{sa_name:"yy", fact:[{fact_name:"zz"}], dim:[{dim_name:"qq"}]}]}})
> db.coll.findOne()
{
"_id" : ObjectId("52a85b44fd0b335ab0546ca4"),
"module" : {
"name" : "xx",
"sa" : [
{
"sa_name" : "yy",
"fact" : [
{
"fact_name" : "zz"
}
],
"dim" : [
{
"dim_name" : "qq"
}
]
}
]
}
}
> db.coll.findOne({"module.sa.fact.fact_name":"zz"})
{
"_id" : ObjectId("52a85b44fd0b335ab0546ca4"),
"module" : {
"name" : "xx",
"sa" : [
{
"sa_name" : "yy",
"fact" : [
{
"fact_name" : "zz"
}
],
"dim" : [
{
"dim_name" : "qq"
}
]
}
]
}
}
Could it be that you're just having problems with some sort of typo as you're querying for "module.sa.fact.name"
where it should be "module.sa.fact.fact_name"
. However, this problem is also true for the query which seems to work for you...
Upvotes: 2
Reputation: 26012
Update your query as :
db.coll.find({"module.sa" : {$elemMatch : {"fact.fact_name": "zz"}}})
Upvotes: 5