NodeJS和MongoDB FindAndModify()需要删除或更新 [英] NodeJS and MongoDB FindAndModify() need remove or update

查看:1142
本文介绍了NodeJS和MongoDB FindAndModify()需要删除或更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用nodejS在mongodb中做一个findAndModifiy,这是我的代码:

im trying to do a findAndModifiy in mongodb with nodejS, This is my code:

var nextBill = function (db, success, log) {
    var collection = db.collection('autoincrements');
    log.debug('autoIncrementRepository', 'nextBill');
    var result = collection.findAndModify({
        query: { _id: 'auto' },
        update: { $inc: { bill: 1 } },
        new: true
    });

    success(result.bill);
};

编辑:

尝试使用回调

collection.findAndModify({
        query: { _id: 'auto' },
        update: { $inc: { bill: 1 } },
        new: true
    }, function (e, result) {
        success(result.budget);
    });

但是给我错误需要删除或更新..但是我这样做..

But give me the error need remove or update..But im doing it..

推荐答案

.findAndModify() 方法与mongo shell实现不同。要进行上述更新,请执行以下操作:

The .findAndModify() method in the node native driver implementation is different from the mongo shell implementation. To do an update as above you do:

collection.findAndModify(
   { "_id": "auto" },
   { "$inc": { "bill": 1 } },
   function(err,doc) {
     // work here

   }
);

奇怪的是要删除你在选项中指定所以同样会删除匹配的文档:

Oddly somewhat to remove you specify in options so the same would "remove" the matched document:

collection.findAndModify(
   { "_id": "auto" },
   { "$inc": { "bill": 1 } },
   { "remove": true },
   function(err,doc) {
     // work here

   }
);

主要区别在于您没有为操作命名关键部分。

The main difference being you do not name the "key" sections for the actions.

这篇关于NodeJS和MongoDB FindAndModify()需要删除或更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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