【问题标题】:How to edit stored data in MongoDB using Express?如何使用 Express 编辑 MongoDB 中存储的数据?
【发布时间】:2020-11-18 20:05:54
【问题描述】:

如何在 MongoDB 中使用 express 作为后端更新文档?

这里是后端 API

app.post("/update",(req,res)=>{
      
    const id=req.body.id;
    console.log(id);
    contact.updateOne({'_id':id},req.body,(err,res)=>{
        if(err) throw err

        console.log('data edited....')

    });

    res.redirect("/show");
    console.log("done..");
});

【问题讨论】:

  • 你想达到什么目的?

标签: node.js mongodb api express crud


【解决方案1】:

我认为您要求更新文档

app.patch("/update", (req, res) => {
    const id = req.body.id;
    console.log(id);
    contact.findByIdAndUpdate(id, req.body, (err, res) => {
        if (err) throw err
        console.log('data edited....')
    });
    res.redirect("/show");
    console.log("done..");
});

希望对你有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2016-05-30
    • 2016-06-12
    • 2023-03-28
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多