.updateOne on MongoDB not working in Node.js

后端 未结 8 994
迷失自我
迷失自我 2021-01-03 20:07

I have the following code:

connection((db) => {
            db.collection(\'orders\')
                .updateOne(
                    { \"_id\": req.body         


        
相关标签:
8条回答
  • 2021-01-03 20:51

    Tough @Spraw's answer is right for some cases, but sometimes it doesn't work. I think the convenient answer is updateOne({_id: new ObjectID(req.body._id)}, {$set: {"name": req.body.name}}, callback).

    the _id in mongodb is a BSON object and should be instantiated.

    0 讨论(0)
  • 2021-01-03 20:51

    This might happen if the collection schema is missing the field that is to be updated. If the schema is not strict (i.e, isStrict: false), check the update operator. In some cases, the new update value can be same as existing value. In this case, mongoose returns

    { n: 1, nModified: 0, ok: 1 }
    
    0 讨论(0)
提交回复
热议问题