node.jsmongodbmongoosedriverfindandmodify

nodejs driver for mongodb fndAndModify


I have the following code:

var conditions = {_id: playerID, 'gameStats.GameID' : gameID},
    update = {$inc: {'gameStats.$.wins' : 1}},
    options = {new : true};

player.findAndModify(conditions, update, options, function (err, doc) {
    if (err) { 
        console.log("updated failed")
        return res.sendStatus(300);     
}

When I execute it the error message "TypeError: undefined is not a function" is returned and I really cannot figure out what is the problem. Can someone help?


Solution

  • The problem is that you are using "mongoose" and there is no .findAndModify() method. Use .findOneAndUpdate() instead:

    player.findOneAndUpdate(conditions, update, options, function (err, doc) {
        if (err) { 
            console.log("updated failed")
            res.sendStatus(300);     
        } else {
          // do things that are happy with the response
        }
    });