簡體   English   中英

如何在Node JS中使用ID更新mongodb數據?

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

我需要在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();
            });
          });
        });

如果您使用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