大家好,这是我的问题情况:
我的类别
模型如下所示:
schema: {
_id: String,
name: String,
overview: String,
courses: [
{
_id: String,
name: String
}
]
}
我在上面的集合中记录了多个类别类别
我需要什么
我有一个[数组],如下所示:Categories._id
:
[
'5812f3cb04700f2563c0e56a',
'5812f3ff04700f2563c0e56b',
...
]
我想要的只是PUSH
这个对象{_id:5812f3ff04700f2563c0e56b, name:'SOMETHING'}
到_id
的数组中列出的所有文档
我尝试过的
Categories.update({
'_id': {
$in : array_of_IDs
}
},
{
$push :{
'courses':{ _id: ret._id , name: ret.title }
}
}, function(err, respp){
if(err) return res.negotiate(err);
else{
req.addFlash('success', 'Categories updated successfully.');
return res.redirect('/new-course');
}
}
);
上面的代码仅更新一个集合(ID 数组中具有第一个 ID 的集合)
请帮忙!
谢谢
最佳答案
可以尝试使用multi: true
作为选项
Categories.update({
'_id': {
$in : array_of_IDs
}
},
{
$push :{
'courses':{ _id: ret._id , name: ret.title }
}
},{ multi: true }, function(err, respp){
//....
}
);
关于node.js - Mongoose,将数据添加/更新到集合的一组文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40301098/