mongoose update with push operations on array and set operation on object
You could try use both the $set
and $push
operators in your update object. Suppose, for example, you want to update both name and address fields in one single operation, use the $set
on the name field and a $push
operation to the address array:
var messages= { title: req.body.title, msg: req.body.msg},query = {'_id': req.body.id},update = { $set: {name: req.body.name}, $push: {messages: message}},options = {upsert: true};Model.findOneAndUpdate(query, update, options, function (err, data) { if (err) { return res.status(500).send(err); } if (!data) { return res.status(404).end(); } return res.status(200).send(data);});