MongoDB更新数组中的嵌套对象

时间:2020-03-21 16:32:51

标签: node.js mongodb mongoose

自三天以来,我一直在解决这个问题。我需要更新数组中的嵌套对象。为了使其更复杂,我的数组本身嵌套在对象中,当用户添加更多类别时,将动态创建键。我遇到两个问题:

  1. 如何在查询中动态创建类别?(通过引用传递值给我一个语法错误)
  2. 如何基于一些过滤器更新特定的Array项

My Document in Mongodb which is to be updated.Object keys internet and electricity has to be dynamically created

对于问题A,我求助于类别的硬编码并继续进行

    switch (category) {
                case 'internet':
                    qry = User.findOneAndUpdate({username: obj.username}, {$push: {"data.internet": newBill}});
                    break;
                case 'electricity':
                    qry = User.findOneAndUpdate({username: obj.username,}, {$push: {"data.electricity": newBill}});
                    break;
                case 'water':
                    qry = User.findOneAndUpdate({username: obj.username}, {$push: {"data.water": newBill}});
                    break;
                case 'gas':
                    qry = User.findOneAndUpdate({username: obj.username}, {$push: {"data.gas": newBill}});
                    break;
                default:

                    break;
            }

创建类别后,我不希望对象在其中重复,因此如果插入重复值,我将使用DAY和Year作为ID来匹配覆盖的意图。我尝试了多种更新方式,但是没有一种有效。请咨询,需要mongoDb专家


console.log("duplicate qry")
            switch (category) {
                case 'internet':
                    qry = User.findOneAndUpdate({username: obj.username}, {$push: {"data.internet": newBill,$position: index}});
                    break;
                case 'electricity':
                    qry = User.updateOne({username: obj.username, month:newBill.month, year:newBill.year}, {$set: {"data.electricity.$": newBill}});
                    console.log("Inside db case "+index)
                    break;
                case 'water':
                    qry = User.findOneAndUpdate({username: obj.username}, {$push: {"data.water": newBill,$position: index}});
                    break;
                case 'gas':
                    qry = User.findOneAndUpdate({username: obj.username}, {$push: {"data.gas": newBill,$position: index}});
                    break;
                default:

                    break;
            }
        }




如果没有重复的对象,我的处理程序将使用两个查询来查找和更新。如果存在,它将返回重复对象的索引,我可以将其用于第二次qry

//insert & update data
app.post('/update', function (req, res) {
    let obj = req.body
    let category = obj.category
    let insertObj = obj.data
    let flag = false;
    let index = 0;
    db.getUserDoc(req.body).then(function (data) {
        // console.log(data.doc)
        let keys = Object.keys(data.doc)
        console.log(`${category}`)
        if (keys.includes(`${category}`)) {
            let catObj = data.doc[`${category}`]
            let nestedKeys = Object.keys(catObj);
            let nestedVals = Object.values(catObj)
            for (let i = 0; i < catObj.length; i++) {
                if (catObj[i].month === insertObj.month && catObj[i].year === insertObj.year) {
                    console.log("not unique " + i)
                    flag = true;
                    index = i;
                    console.log(catObj.length);
                }
            }
            // console.log(insertObj.month + " "+ insertObj.year)
            console.log(catObj.length);
        }
        db.update(req.body, obj.data, obj.category, flag, index).then(function (data) {
            // res.setHeader('Content-Type', 'text/plain');
            // res.end(data)
            console.log(data);
            res.end(JSON.stringify(200));
        }).catch(function (e) {
            return res.status(500, {
                error: e
            })
        });

        // console.log(typeof data)
        // console.log(data);
        // res.end(JSON.stringify(data.doc));
    })


})



0 个答案:

没有答案