NodeJS 和 MongoDB FindAndModify() 需要删除或更新 [英] NodeJS and MongoDB FindAndModify() need remove or update
本文介绍了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()
方法在 node 原生驱动实现中不同于 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屋!
查看全文