简化forEach语句以使用嵌套对象和数组更新mongoDB文档

时间:2019-02-11 20:00:54

标签: javascript mongodb performance query-performance

我想在以下文档中更新键shouldSendAlert的值:

{
    "_id" : ObjectId("5c61c4db46d18e1092c5b024"),
    "service" : "SRVPVD",
    "menu" : [ 
        {
            "sub" : [ 
                {
                    "options" : [ 
                        {
                            "item" : [ 
                                {
                                    "name" : "",
                                    "actions" : [ 
                                        {
                                            "name" : "communicateClient",
                                            "value" : true
                                        }, 
                                        {
                                            "name" : "shouldSendAlert",
                                            "value" : false
                                        }
                                    ]
                                }
                            ],
                            "name" : "Technology Support"
                        }, 
                        {
                            "item" : [ 
                                {
                                    "name" : "",
                                    "actions" : [ 
                                        {
                                            "name" : "communicateClient",
                                            "value" : true
                                        }
                                    ]
                                }
                            ],
                            "name" : "Company Support"
                        }
                    ],
                    "name" : "Support"
                }, 
                {
                    "name" : " FAQ"
                }
            ],
            "name" : "Help"
        }
    ]
}

我已经做到了,使用多个$elemMatch查询文档,并使用forEach遍历嵌套数组以更改shouldSendAlert的值:

{
    let menuItems = db.getCollection('menumodels').find({menu: {$elemMatch: {name: 'Help',sub: {$elemMatch: {name: 'Support',motivos: {$elemMatch: {name: 'Technology Support'}}}}}}});

    menuItems.forEach((r) => {
        r.menu.forEach(menuItem => {
            if (menuItem.name == 'Help') {
                menuItem.sub.forEach(sub => {
                    if (sub.name == 'Support') {
                        sub.motivos.forEach(motivo => {
                            if (motivo.name == "Technology Support") {
                                motivo.item[0].actions.forEach(action => {
                                    if (action.name == 'shouldSendAlert') {
                                        action.value = true;
                                        db.getCollection('menumodels').update({_id: r._id}, {$set: {menu: r.menu}})
                                    }
                                })
                            }
                        })
                    }
                })
            }
        })
    });
}

对性能进行编码是否必要-对该MongoDB查询进行编码 还是将逻辑更新为更智能的形式? $elemMatch的多次使用会影响性能吗?

0 个答案:

没有答案