不能用其他id,MongoDB,NodeJS更新两块板

时间:2017-12-30 21:54:38

标签: node.js mongodb

我尝试进行两次更新,直到这一刻我这样做,可以somoene更正此代码或解释我如何只使用一个查询进行两次更新?

Board.findOneAndUpdate({ _id: transferObj.idBoard },
    {
        $set: {
            ['lists.' + transferObj.fromIndexList + '.cards']: transferObj.fromCards,
        }
    },
    {
        upsert: true
    },
    Board.findOneAndUpdate({ _id: transferObj.toBoard },
        {
            $set: {
                ['lists.' + transferObj.toList + '.cards']: transferObj.toCards,
            }
        },
        {
            upsert: true
        },
        ((err, updated) => {
            if (err) { console.log(err) }
            else { res.status(200).send('Deleted'); }
        })

1 个答案:

答案 0 :(得分:0)

也许这有用吗?

Board.bulkWrite(
    [
        {
            updateOne: {
                filter: { _id: transferObj.idBoard },
                update: {
                    ['lists.' + transferObj.fromIndexList + '.cards']: transferObj.fromCards
                }
            }
        },
        {
            updateOne: {
                filter: { _id: transferObj.toBoard },
                update: {
                    ['lists.' + transferObj.toList + '.cards']: transferObj.toCards
                }
            }
        }
    ],
    function (err, result) {
        if (err) {
            console.log(err)
        } else {
            console.log(result)
        }
    }
);