Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  28] [ 1]  / answers: 1 / hits: 23077  / 10 Years ago, fri, june 20, 2014, 12:00:00

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);
};


EDIT:



Try with callback



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..


More From » node.js

 Answers
25

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.


[#70497] Wednesday, June 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
katia

Total Points: 570
Total Questions: 101
Total Answers: 85

Location: Saudi Arabia
Member since Sat, Aug 20, 2022
2 Years ago
;