在节点中使用mongoosejs更新多个记录 [英] update multiple records using mongoosejs in node

查看:66
本文介绍了在节点中使用mongoosejs更新多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用mongoosejs和node更新多个记录.由于某些原因,即使有多个匹配项,我也只会更新一条记录.我还注意到,在.update()之后,回调不会触发.我没有收到任何错误消息.这是怎么回事?

I'm having trouble updating multiple records using mongoosejs and node. For some reason, I only update a single record, even if multiple match. I've also noticed that the callback will not fire after .update(). I'm not getting any error messages. What's going on here?

Page.find({status:'queued'})
    .limit(queue_limit-queue.length)
    .update({ status: 'active' },{ multi: true },function(err,num){
        console.log("updated "+num);
        //this callback will never fire, but a single record will be updated and marked as active.                                                                                                 
    });

推荐答案

Query#update 不会"不能接受options参数,但 Model.update 可以.因此,您希望将其重写为:

Query#update doesn't accept an options parameter, but Model.update does. So you'd want to rewrite this as:

Page.update({status:'queued'}, {status: 'active'}, {multi: true}, 
    function(err, num) {
        console.log("updated "+num);
    }
);

我不确定您要使用链中的limit调用做什么,但是您不能在更新中使用它.

I'm not sure what you were trying to do with the limit call in the chain, but you can't use that in an update.

更新

以上查询将更新{status: 'queued'}处的所有文档. update的唯一选择只是第一个匹配的{multi: false}或所有匹配的{multi: true}.

The above query will update all docs where {status: 'queued'}. Your only choices with update are just the first matching one {multi: false} or all matches {multi: true}.

听起来像您需要重新整理以便一次将文档从队列中移出并切换到 findOneAndUpdate 而不是update,因此您可以访问已从'queued'升级到'active'的文档.

Sounds like you need to rework things to take docs off your queue one at a time and switch to findOneAndUpdate instead of update so you have access to the doc you've updated from 'queued' to 'active'.

这篇关于在节点中使用mongoosejs更新多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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