0

How do I query a deeply nested docs like show in the below image.

enter image description here

Here columns is an array of unknown size. Each element in the column contains a record that is again an array. Each element of the record array contains an array called fields. Each entry in the field contains 2 keys called name and value.

I'm querying the name of the innermost array (in fields array). I couldn't go above level 1 order of nesting.

JSON doc of the above image

"data" : {
        "columns" : [
            {
                "name" : "styleId", 
                "record" : [
                    {
                        "fname" : "column_mapping", 
                        "_id" : ObjectId("5ba488c79dc6d62c90257752"), 
                        "fields" : [
                            {
                                "name" : "column_mapping_form", 
                                "value" : "styleId"
                            }
                        ], 
                        "rules" : [
                            [

                            ]
                        ]
                    }
                ]
            }, 
            {
                "name" : "vendorArticleNumber", 
                "record" : [
                    {
                        "fname" : "column_mapping", 
                        "_id" : ObjectId("5ba488c79dc6d62c90257753"), 
                        "fields" : [
                            {
                                "name" : "column_mapping_form", 
                                "value" : "vendorArticleNumber"
                            }
                        ], 
                        "rules" : [
                            [

                            ]
                        ]
                    }
                ]
            }, 
            {
                "name" : "vendorArticleName", 
                "record" : [
                    {
                        "fname" : "column_mapping", 
                        "_id" : ObjectId("5ba488c79dc6d62c90257754"), 
                        "fields" : [
                            {
                                "name" : "column_mapping_form", 
                                "value" : "vendorArticleName"
                            }
                        ], 
                        "rules" : [
                            [

                            ]
                        ]
                    }
                ]
            }
}

What can be the solutions if such kind of heaving nesting is there?

3
  • Use don't notation docs.mongodb.com/manual/tutorial/query-array-of-documents and avoid posting images when you can post a json document as code snippet. Commented Sep 26, 2018 at 10:57
  • I have added the json Commented Sep 26, 2018 at 11:17
  • Please give an example, am not able to go above 2 levels of nesting Commented Sep 26, 2018 at 11:22

1 Answer 1

3
db.collection.find("data.columns.record.fields.name" : "column_mapping_form")

will match all documents where there is at least one element of columns has at least one record with at least one field where name is "column_mapping_form".

https://docs.mongodb.com/manual/tutorial/query-array-of-documents/ has very good explanation, examples, and interactive shell to play with.

Sign up to request clarification or add additional context in comments.

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.