15

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

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
colymore
  • 11,776
  • 13
  • 48
  • 90
  • Which driver are you using? The MongoDB node native driver requires a callback, so does mongoose, so does monk and others. – Neil Lunn Jun 20 '14 at 11:13
  • Native driver, but using callback i get the same error:. I will edit my awnser with my actually code. – colymore Jun 20 '14 at 11:14

4 Answers4

21

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.

Shimon S
  • 4,048
  • 2
  • 29
  • 34
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • 1
    Thanks this works, but why have they differed from the shell spec / what the docs say?? – Aodh Jun 27 '15 at 15:53
  • 1
    @Aodh the shell spec is not a spec for driver implementations. The docs you're reading are for mongo itself, not the driver. Read the driver documentation found here: http://mongodb.github.io/node-mongodb-native/ – wallacer Jul 06 '15 at 23:52
  • Hi, am still getting error, here is my code db.collection(nextIDCollection).findAndModify({"_id":1},{$inc: {"nextID":1}} ,function(err,doc){}); I could not find whats wrong with the code. – Vishwanath gowda k Oct 19 '15 at 06:50
10

http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify

This above document specifies that the second parameter is the sort order for choosing which document to use if multiple ones match the query. Only giving two parameters will result in the "need remove or update" error message.

collection('MyCollection').findAndModify(
    { _id: "auto" },
    [],
    { $inc: { "bill": 1 } },
    { upsert: true, new: true },
    function(err,doc) {
       // work here
    }
);
Paul Van Camp
  • 261
  • 3
  • 6
2
Hi I have followed this and it worked perfectly.

db.collection('test').findAndModify(
  {hello: 'world'}, // query
  [['_id','asc']],  // sort order
  {$set: {hi: 'there'}}, // replacement, replaces only the field "hi"
  {}, // options
  function(err, object) {
      if (err){
          console.warn(err.message);  // returns error if no matching object found
      }else{
          console.dir(object);
      }
  });
});
0

Try this It worked for me in nodejs

users.findAndModify(
           { "_id": userid,"password":pwd},
           [['_id', 'asc']],
           { "$set":{"password":npwd}},
           {"upsert":false}
        ,function(err,result){
        //enter code here

    })