0

I need to update the data from client side in mongodb but I can see clicked updated Id value in node js but it does not update in mongodb wat to do.. help me to update the values in mongo db using id values

router.post('/datapassfup', (req, res) => {
        console.log("updated values are",req.body)
        MongoClient.connect(url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("mohan");
            var myquery = { id: req.body.id };
            var newvalues = { $set: {name: req.body.name, username: 
            req.body.username } };
            dbo.collection("customers").updateMany(myquery,newvalues, 
            function(err, res) {
              if (err) throw err;
              console.log("1 document updated");
              db.close();
            });
          });
        });
5
  • in the consle window i can see the updated values in node js Commented Jul 17, 2019 at 10:31
  • 1
    i think { id: req.body.id }; should be {_id: req.body.id }; Commented Jul 17, 2019 at 10:56
  • db.collection.deleteOne({_id: req.body.id}) Commented Jul 17, 2019 at 11:23
  • sorry for my mistake. I need updation code Commented Jul 17, 2019 at 11:25
  • Can you give a bit of context on the update operation: what does a document from customers collection look like, can the id field map to may documents or it's unique? If the latter is true then updateMany is not appropriate for that case. Commented Jul 17, 2019 at 12:17

1 Answer 1

1

if you use the mongodb id for you query then you need to create a new objectid for _id search

const {ObjectId} = require("mongodb");


const query = {_id:new ObjectId(req.body.id)}

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

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.