简体   繁体   中英

Query for deeply nested array in mongodb

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

在此处输入图片说明

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM