43

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...')"

3
  • The documentation is fine just always look for the "examples" section: try { db.restaurant.updateOne( { "name" : "Central Perk Cafe" }, { $set: { "violations" : 3 } } ); } catch (e) { print(e); } Commented Dec 5, 2017 at 15:02
  • Hmm I'm talking about the documentation related to node.js. I can't write queries like that in there (or can I?) Commented Dec 5, 2017 at 15:05
  • can u please share 2 3 mongo documents? Also what will be value of req.body._id? Commented Dec 5, 2017 at 15:10

9 Answers 9

72

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe it's because there is no match with your filter ? Can you try to find a document with { "_id": req.body._id} ?
Hmm the ID passed in req.body._id is the same as the ID in the database, however it is passed as a string, while in the database, it is of type ObjectId(). Could it be that it's not filtering it.because of that?
11

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

1 Comment

I can't understand the logic, but for find and updateOne you should use different syntacsis for search by id. Samples: - db.admins.find({ id: req.params.id, isDeleted: false }) and db.admins.updateOne( { _id: new ObjectID(req.params.id), isDeleted: false}. Simple question - WHY?
8

The correct syntax is:

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

UPDATE: Not ObjectID but ObjectId.

1 Comment

Hey @victor thanks for the answer but would you go more into detail please. It would be useful to know where OP is actually making a mistake in their syntax.
5

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 }

Comments

4

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.

1 Comment

That seemed to be the case for me, it only worked when I added the callback function.
1

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.

Comments

-1

I had this trouble too, I create a constant with _id from req.body

const {_id} = req.body  //pass ID user
await User.updateOne({_id},{$set:data}, (err)=>{
            if(err) return res.status(200).json({
                error: true,
                code: 115,
                message: "Erro to update user!"
            })
        })

1 Comment

You can assign _id directly to updateone method. Like - User.updateOne({req.body._id},{$set:data}
-1

En NEXT.js

My project is on React and Next.js. I had this issue. My silly solution was Restart with (next dev) xD

I added two fields to my Schema model and I tried to update that new fields, but did not works until i restart mi project.

I hope to save time for someone. :)

Comments

-1

after spending couple of hours on it I found out that for update I had to convert the id value to Integer, this was however not required where only find was being used

dbCollection.updateOne({ _id: parseInt(reqBody.id) }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.