简体   繁体   中英

How do i update mongodb data using id in node js?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM