.updateOne on MongoDB not working in Node.js

2020-04-02 01:35发布

I have the following code:

connection((db) => {
            db.collection('orders')
                .updateOne(
                    { "_id": req.body._id}, // Filter
                    {"name": req.body.name} // Update
                )
                .then((obj) => {
                    console.log('Updated - ' + obj);
                    res.redirect('orders')
                })
                .catch((err) => {
                    console.log('Error: ' + err);
                })
        })

I want to change the name in the order but it doesn't update it. The result in the console is

Updated - {"n":0,"nModified":0,"ok":1}

I tried to read the documentation but it's horrific

EDIT: {$set: {"name": req.body.name}}, didn't work as well

EDIT 2: The passed ID matches the _id in the database. Could it be a problem that I'm querying for plain text ID while in the database it is referred to as "ObjectId('5a42ja...')"

6条回答
走好不送
2楼-- · 2020-04-02 02:13

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.

查看更多
Luminary・发光体
3楼-- · 2020-04-02 02:20

Use {$set: {"name": req.body.name}} (as Sparw mentioned) to update the the properties you want in the document. Also, if the id that you pass as a parameter does not exists in the collection (and you want to create one with the same id) you can pass as a third parameter {upsert: true} to create one.

In your example:

connection((db) => {
          db.collection('orders')
               .updateOne(
                  { "_id": req.body._id}, // Filter
                  {$set: {"name": req.body.name}}, // Update
                  {upsert: true} // add document with req.body._id if not exists 

             )
            .then((obj) => {
               console.log('Updated - ' + obj);
              res.redirect('orders')
         })
        .catch((err) => {
           console.log('Error: ' + err);
      }) })
查看更多
霸刀☆藐视天下
4楼-- · 2020-04-02 02:29

The correct syntax is:

monDb.collection.updateOne(
    {"_id": ObjectID(req.params.id)}, 
    { $set: updateDoc }, 
    function(err, doc) {
      ...
    }
);
查看更多
老娘就宠你
5楼-- · 2020-04-02 02:29

Too late but for newbies or students learning nodejs with mongodb. instead of updateOne method, just use the update method.

connection((db) => {
          db.collection('orders')
               .update(
                  { "_id": req.body._id}, // Filter
                  {$set: {"name": req.body.name}}, // Update
                  {upsert: true} // add document with req.body._id if not exists 

             )
            .then((obj) => {
               console.log('Updated - ' + obj);
              res.redirect('orders')
         })
        .catch((err) => {
           console.log('Error: ' + err);
      }) })
查看更多
ゆ 、 Hurt°
6楼-- · 2020-04-02 02:33

Maybe you should use "$set" in your update query like this :

{$set: {"name": req.body.name}}, // Update

More information in documentation

EDIT

If it doesn't work, this is probably because there is no match with your filter.

Maybe you should try to match with an ObjectId like this :

var ObjectID = require('mongodb').ObjectID;

// In your request
{ "_id": ObjectID(req.body._id)}, // Filter

Hope it helps.

查看更多
够拽才男人
7楼-- · 2020-04-02 02:37

For me, I have to delete the "_id"/id field before passing the object in the update.

Or it will say that the field is invalid.

Obviously, updated the key while you're using it as a reference isn't the best thing to do.

查看更多
登录 后发表回答