Tom Maton
Tom Maton

Reputation: 1594

mongoDB findAndModify error - nodejs

I'm running into an error when I'm trying to run the findAndModify() method on a mongoDB database on Node.

The error I'm getting is:

[MongoError: exception: must specify remove or update]

Which I find weird as I've specified 'update', my code is as follows.

var techId = req.params.id,
    collection = db.collection('tech');

collection.findAndModify({
    query: { _id: techId},
    update: { $inc: { score: 1 } }
}, function(err, doc){
    console.log(err, doc);
});

Upvotes: 0

Views: 2441

Answers (1)

dersvenhesse
dersvenhesse

Reputation: 6424

You're using the syntax wrong, there are too much objects.

Try:

db.collection('tech').findAndModify(
    {_id: techId}, // query
    {$inc: { score: 1 }}, // update
    function(err, object) {
        console.log(err, doc);
    }
);

http://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html

Upvotes: 2

Related Questions