我尝试进行两次更新,直到这一刻我这样做,可以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'); }
})
答案 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)
}
}
);