【问题标题】:How to update data into mongodb using mongoose如何使用 mongoose 将数据更新到 mongodb
【发布时间】:2020-05-27 03:33:29
【问题描述】:

尝试使用 mongoose 和 nodejs 更新 mongodb 中的值但不工作。我不知道该怎么做。如果有人知道,请帮助我找到解决方案。

data.controller.js:

module.exports.updateData = (req, res, next) => { 
 var uproducts = new updateProduct({
    product_name: collectionDataJSON.product_name 
}); 
updateProduct.updateOne({ p_id: thispid }, uproducts, function(err, raw) {
    if (err) {
        res.send(err);
    }
    res.send(raw);
});
}

【问题讨论】:

  • 你有什么错误吗?
  • @Yousaf:不……没有得到
  • 我在猫鼬中使用 findOne 和保存方法得到了解决方案

标签: node.js mongodb mongoose


【解决方案1】:

你不需要传递一个猫鼬对象给更新方法,你只需要传递一个普通的对象

类似的东西

module.exports.updateData = (req, res, next) => {
    var uproducts = { // a normal object
        product_name: collectionDataJSON.product_name
    };
    updateProduct.updateOne(
        { p_id: thispid }, // filter part
        { $set: uproducts }, // update part
        function (err, raw) { // call back
            if (err) {
                res.send(err);
            }
            res.send(raw);
        }
    );
}

希望对你有帮助

【讨论】:

猜你喜欢
  • 2017-01-06
  • 1970-01-01
  • 2020-11-16
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2017-08-30
相关资源
最近更新 更多