Mongoose, Add/Update 数据到集合的一组文档

Mongoose, Add/Update data to a set of documents of a collection

大家好,我的问题是这样的:

我的Categories模型是这样的:

schema: {   
    _id: String,
    name: String,
    overview: String,
    courses: [
        {
            _id: String,
            name: String
        }
    ]
}

我记录了多个分类,在上面的集合中Categories

我需要什么

我有一个 [ 数组 ]Categories._id 像这样:

[
  '5812f3cb04700f2563c0e56a',
  '5812f3ff04700f2563c0e56b',
  ...
]

我想要的只是PUSH这个对象{_id:5812f3ff04700f2563c0e56b, name:'SOMETHING' }到[中列出的所有文件=40=]数组 of _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){
        //....
    }
);