returnOriginal mongoose code example

Example 1: mongoose find and update prop

var query = {'username': req.user.username};
req.newData.username = req.user.username;

MyModel.findOneAndUpdate(query, req.newData, {upsert: true}, function(err, doc) {
    if (err) return res.send(500, {error: err});
    return res.send('Succesfully saved.');
});

Example 2: returned value by findOneAndUpdate

const filter = { age: 17 };
const doc = { $set: { name: "Naomi" } };
const options = { new: true };

Cat.findOneAndUpdate(filter, doc, options, (err, doc) => {
    if (err) console.log("Something wrong when updating data!");
    console.log(doc);
});

Tags:

Misc Example