嘿我想在Api NodeJS中使用MongoDB的push函数将字符串添加到嵌套数组中。
router.post('/add-member-to-card', (req, res) => {
Board.findOneAndUpdate({ _id: req.body.idBoard },
{
"$push": { ['lists.' + req.body.indexList + '.cards.' + req.body.indexCard + '.members'] : { $each: req.body.member, $position: 0 } },
"$push": { "activity" : { $each: [req.body.activity], $position: 0 } },
},
{ upsert: true },
((err, updated) => {
if (err) { console.log(err) }
else {
console.log(req.body.member);
}
})
);
});
第二次推动正在发挥作用,但首先是错误的。一般来说,我正在尝试这样做,也许不能这样做?我的意思是将项添加到嵌入式数组中?
req.body.member的容器是 - > da7sda68ds6ad87asd68da
修改
我带着第一个$ push:
// "$set": { ['lists.' + req.body.indexList + '.cards.' + req.body.indexCard + '.members']: req.body.members },
工作/ \
答案 0 :(得分:0)
问题可能是您对字段$push
有多个定义。这与以下相同:
let foo = {
a: 1,
a: 2
};
尝试将其写为:
{
$push: {
['lists.' + ... + '.members'] : { $each: req.body.member, $position: 0 },
"activity" : { $each: [req.body.activity], $position: 0 }
},
}
也许这会有所帮助