将值插入嵌套数据mongodb中的数组

时间:2019-05-15 08:49:28

标签: mongodb

我有一个数据集,文档看起来像这样。

{
    "_id" : ObjectId("5c9873aa0fd62b0c9c84e749"),
    "account_id" : "1",
    "company_tree" : [
       {
            "id" : "5d561a07-6d2c-45ae-b249-757aa41e9b2b",
            "name" : "dept12",
            "privacy_advisor" : {
                "name" : "wfewf",
                "email" : "fwefw",
                "phone" : "fwef"
            },
            "controller" : {
                "name" : "qwqwerf",
                "email" : "fwefew",
                "phone" : "fwefwe"
            },
            "index" : 1,
            "items" : [ 
                {
                    "id" : "a2aae8b2-24d1-44ee-98a6-29d78835fcfd",
                    "name" : "dept3",
                    "privacy_advisor" : {
                        "name" : "wfewf",
                        "email" : "fwefw",
                        "phone" : "fwef"
                    },
                    "controller" : {
                        "name" : "qwqwerf",
                        "email" : "fwefew",
                        "phone" : "fwefwe"
                    },
                    "index" : 0,
                    "users" : [ 
                        {
                            "id" : "1",
                            "name" : "admin",
                            "email" : "admin@admin.com",
                            "primary_locale" : "nb",
                            "role_membership" : {
                                "id" : 1,
                                "active_status" : true,
                                "name" : "leader"
                            }
                        }, 
                        {
                            "id" : "f18ed780-c3c4-4a43-b1e2-f7b5deef2660",
                            "name" : "user",
                            "email" : "tromesh@tromesh.com",
                            "primary_locale" : "nb",
                            "role_membership" : {
                                "id" : 1,
                                "active_status" : true,
                                "name" : "user"
                            }
                        }
                    ],
                    "items" : [ 
                        {
                            "id" : "83923ba0-8d0f-4c47-8953-0f9a86a7fcbf",
                            "name" : "dept4",
                            "privacy_advisor" : {
                                "name" : "wfewf",
                                "email" : "fwefw",
                                "phone" : "fwef"
                            },
                            "controller" : {
                                "name" : "qwqwerf",
                                "email" : "fwefew",
                                "phone" : "fwefwe"
                            },
                            "index" : 0,
                            "users" : [ 
                                {
                                    "id" : "1",
                                    "name" : "admin",
                                    "email" : "admin@test.com",
                                    "primary_locale" : "nb",
                                    "role_membership" : {
                                        "id" : 1,
                                        "active_status" : true,
                                        "name" : "leader"
                                    }
                                }, 
                                {
                                    "id" : "f18ed780-c3c4-4a43-b1e2-f7b5deef2660",
                                    "name" : "user",
                                    "email" : "tromesh@tromesh.com",
                                    "primary_locale" : "nb",
                                    "role_membership" : {
                                        "id" : 2,
                                        "active_status" : true,
                                        "name" : "user"
                                    }
                                }
                            ],
                            "items" : [ 
                                {
                                    "id" : "34bfbd43-0d48-4ccf-a1a8-27770ee9048f",
                                    "name" : "dept5",
                                    "privacy_advisor" : {
                                        "name" : "wfewf",
                                        "email" : "fwefw",
                                        "phone" : "fwef"
                                    },
                                    "controller" : {
                                        "name" : "qwqwerf",
                                        "email" : "fwefew",
                                        "phone" : "fwefwe"
                                    },
                                    "index" : 0,
                                    "items" : [ 
                                        {
                                            "id" : "1b8f13af-ff80-46c1-ad59-df454268b4fd",
                                            "name" : "dept6",
                                            "privacy_advisor" : {
                                                "name" : "wfewf",
                                                "email" : "fwefw",
                                                "phone" : "fwef"
                                            },
                                            "controller" : {
                                                "name" : "qwqwerf",
                                                "email" : "fwefew",
                                                "phone" : "fwefwe"
                                            },
                                            "index" : 0,
                                            "statistics" : {
                                                "insight" : {
                                                    "value" : "88"
                                                },
                                                "privacyQuality" : {
                                                    "value" : "44"
                                                }
                                            }
                                        }
                                    ],
                                    "expanded" : true,
                                    "statistics" : {
                                        "privacyQuality" : {
                                            "value" : "56"
                                        }
                                    }
                                }
                            ],
                            "expanded" : true,
                            "selected" : true,
                            "isAccount" : true
                        }
                    ],
                    "expanded" : true,
                    "statistics" : {
                        "insight" : {
                            "value" : "44"
                        },
                        "privacyQuality" : {
                            "value" : "89"
                        }
                    },
                    "isAccount" : true,
                    "details" : {
                        "company_name" : "New Company"
                    },
                    "public_id" : "e4f5ed70-3be6-11e9-b57b-ed1bd06820e4",
                    "internal_id" : "e4f5ed70-3be6-11e9-8fde-3df658dfaac7",
                    "is_public_link_published" : true,
                    "theme" : {
                        "primary" : "#3c99df",
                        "secondary" : "#5cc1ed",
                        "tertiary" : "#2176b6"
                    }
                }
            ],
            "expanded" : true,
            "statistics" : {
                "insight" : {
                    "value" : "75"
                }
            }
        },
 //more objects with nested properties.
     ];

我需要将数据插入到company_tree数组中对象的“ users”数组中,以检查name属性(“ name”:“ dept12”)。 company_tree数组中的每个对象在“ items”中都有嵌套的对象。我已经有文档_id,用户数据,部门名称(“名称”:“ dept12”)。 这是我尝试过的代码,即时消息卡在这里。如果有人可以指导我如何进行更新,那就太好了。

db.getCollection('organization').find({"_id" : ObjectId("5cdbc8d4a3a4280fe86b9085")},{"company_tree":1,'_id': false}).toArray(function (err, result) {
        console.log(result); 
    };

0 个答案:

没有答案