Does Mongoose support the Mongodb `findAndModify` method?

前端 未结 9 2156
[愿得一人]
[愿得一人] 2020-11-29 23:57

I would like to use findAndModify to atomically increment a field, using Mongoose.

However, the code below throws the error \"TypeError: Object # has no method \'fin

9条回答
  •  天命终不由人
    2020-11-30 00:25

    In version 3, the mongoose findOneAndUpdate method exposes mongodb's findAndModify operation. It works like so:

    var query = { name: 'Sprinkls' };
    var update = { name: 'Sprinkles' };
    var options = { new: false };
    Cat.findOneAndUpdate(query, update, options, function (err, cat) {
      if (err) ..
      render('cat', cat);
    });
    

    More info here: http://aaronheckmann.tumblr.com/post/48943524629/mongoose-v3-part-2-findandmodify

提交回复
热议问题