2

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.

1
  • The document is invalid, an array cannot directly contain fields, i.e. fact : [ fact_name : "zz" ] should be fact : [ { fact_name : "zz" } ] Commented Dec 11, 2013 at 12:43

2 Answers 2

5

Update your query as :

db.coll.find({"module.sa" : {$elemMatch : {"fact.fact_name": "zz"}}})
Sign up to request clarification or add additional context in comments.

1 Comment

This works, but it solves the problem by fixing the typo(s), not because of $elemMatch. In fact, $elemMatch is designed for matching multiple criteria on a single array element which is not required here. Also, note that this still uses a multikey because in the OP's schema fact is an array itself, so it's arguable where one would want to use the $elemMatch.
2

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.