Mongoose多个$推到多个数组
我正在尝试更新Mongodb中的多个数据库条目:
User.findByIdAndUpdate(req.user._id, {$push: {people_seen: person_id}}, {$push: {people_liked: person_id}}, function(err, usr){ console.log(req.user); res.json({success: true}); });
但是,只有people_seen
得到保存。
您多次使用$push
操作符。 findByIdAndUpdate
的第二个参数是一个选项文档。 正确的$push
的语法是:
{ $push: { <field1>: <value1>, ... } }
在您的查询中正确的查询是:
User.findByIdAndUpdate(req.user._id, { $push: { people_seen: person_id, people_liked: person_id }}, function(err, usr){ console.log(req.user); res.json({ success: true }); });