Using mongoose to add new field to existing document in mongodb through update method
Try the following code:
User.update(
{uid: 'uid'},
{vehicle_status : 'vehicleSatus' },
{multi:true},
function(err, numberAffected){
});
Also, make sure that you add vehicle_status to the schema.
Going through this solution in Year 2019.
Please note: collection.update
is deprecated. Use updateOne
, updateMany
, or bulkWrite
instead
According to new scenario always use {upsert:true}.
upsert - if set to true and no record matched to the query, replacement object is inserted as a new record.
user.updateOne(
{ email: req.body.email },
{ $set: { name: 'Aaakash'}},{upsert:true}).then((result, err) => {
return res.status(200).json({ data: result, message:"Value Updated" });
})