简体   繁体   English

如何在Node JS中使用ID更新mongodb数据?

[英]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 我需要在mongodb中从客户端更新数据,但是我可以在node js中看到单击的已更新ID值,但是在mongodb wat中却无法更新。.帮助我使用id值更新mongo db中的值

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 如果您使用mongodb id进行查询,则需要为_id搜索创建一个新的objectid

 const {ObjectId} = require("mongodb"); const query = {_id:new ObjectId(req.body.id)} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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