通过ID集更新多个文档.猫鼬 [英] Update multiple documents by id set. Mongoose

查看:69
本文介绍了通过ID集更新多个文档.猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道猫鼬是否有某种方法可以通过ID设置来更新多个文档.例如:

I wonder if mongoose has some method to update multiple documents by id set. For example:

for (var i = 0, l = ids.length; i < l; i++) {
    Element.update({'_id': ids[i]}, {'visibility': visibility} ,function(err, records){
        if (err) {
            return false;
        } else {
            return true;
        };
    });
};

我想知道的是,如果猫鼬可以做这样的事情:

What i want to know, that if mongoose can do something like this:

Element.update({'_id': ids}, {'visibility': visibility}, {multi: true} ,function(err, records){
    if (err) {
        return false;
    }
});

其中id是id的数组,例如['id1','id2','id3']-样本数组. 查找同样的问题.

where ids is an array of ids, like ['id1', 'id2', 'id3'] - sample array. Same question for find.

推荐答案

最可能是的.在mongodb查询中使用 $ in 运算符进行调用.

Most probably yes. And it is called using $in operator in mongodb query for update.

db.Element.update(
   { _id: { $in: ['id1', 'id2', 'id3'] } },
   { $set: { visibility : yourvisibility } },
   {multi: true}
)

所有您需要的是找到如何在猫鼬中实现$ in.

All you need is to find how to implement $in in mongoose.

这篇关于通过ID集更新多个文档.猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆