Sergio Cano
Sergio Cano

Reputation: 760

update and retrieve the updated document mongodb

I am trying to update a document with nested subdocuments, but i always retrieve the previus document.

i tried

{ returnOriginal: false }

but it is not working...

this is my code in nodejs

almacenCtrl.updateAlmacen = async (req, res) => {
    almacen = await almacenModel.findOneAndUpdate(req.params.id, { $set: req.body }, { returnOriginal: false }, function (err, updated) {
        res.json(updated)
    })
}

what am i doing wrong?

//After update i check with mongoshell and the update was updated successfully

Upvotes: 0

Views: 34

Answers (1)

Nishu Tayal
Nishu Tayal

Reputation: 20830

Use {new : true} as given below:

almacenCtrl.updateAlmacen = async (req, res) => {
    almacen = await almacenModel.findOneAndUpdate(req.params.id, { $set: req.body }, { new: true }, function (err, updated) {
        res.json(updated)
    })
}

Upvotes: 2

Related Questions