Error: the update operation document must contain atomic operators, when running updateOne

后端 未结 4 560
北恋
北恋 2020-12-05 03:41

In my collection, there is only one document.

> db.c20160712.find()
{ \"_id\" : ObjectId(\"57ab909791c3b3a393e9e277\"), \"Dimension_id\" : 2, \"Attribute\         


        
相关标签:
4条回答
  • 2020-12-05 04:14

    I believe this was changed as a side-effect of introducing the updateOne() method in addition to update() and updateMany() as somewhat of a safeguard to prevent user's from accidentally overriding an entire document.

    You can use the replaceOne() method instead, or an update() without specifying multi:true.

    0 讨论(0)
  • You did the same mistake as I did. Upon going through the docs I realized the syntax is wrong. Try:

    db.c20160712.updateOne( 
       { "Attribute" : "good"}, 
       {"Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Genre" : "Action"}, 
       { upsert: true} 
    )
    
    0 讨论(0)
  • 2020-12-05 04:22

    You should use this code because I was also facing the same problem and then I used this code:

    updateOne(
        { _id: new ObjectID(req.params.id) },
        { $set: { title: req.body.bookName, author: req.body.authorName } },
        { upsert: true }
    )
    

    and you should also define ObjectID otherwise the problem will occur again.

    const ObjectID = require('mongodb').ObjectID;
    
    0 讨论(0)
  • 2020-12-05 04:26

    Wrong syntax for the second parameter. Please check the docs. It should be:

    db.c20160712.updateOne(
        { "Attribute" : "good" }, 
        { $set: {"Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Genre" : "Action" } },
        { upsert: true }
    );
    
    0 讨论(0)
提交回复
热议问题