.updateOne on MongoDB not working in Node.js

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.


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.


The correct syntax is:

monDb.collection.updateOne(
    {"_id": ObjectId(req.params.id)}, 
    { $set: updateDoc }, 
    function(err, doc) {
      ...
    }
);

UPDATE: Not ObjectID but ObjectId.


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