简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM