Pappa S
Pappa S

Reputation: 343

How to update data into mongodb using mongoose

Tried to update value in mongodb using mongoose and nodejs but not working.I do not know how to do it. If anyone know please help me to find solution.

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

Upvotes: 1

Views: 332

Answers (1)

Mohammed Yousry
Mohammed Yousry

Reputation: 2184

You don't need to pass a mongoose object to the update method, you just need to pass a normal object

something like this

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

hope it helps

Upvotes: 1

Related Questions