Add a field to existing MongoDB document (with Mongoose in Node.js) Add a field to existing MongoDB document (with Mongoose in Node.js) mongoose mongoose

Add a field to existing MongoDB document (with Mongoose in Node.js)


try

Article.update(     {link: 'http://www.atlantico.fr/example.html'},      {day : 'example' },     {multi:true},        function(err, numberAffected){         });

and don't forget to add day to schema.


await Users.updateOne( {link: 'http://www.atlantico.fr/example.html'},{ $set: { day : 'example'} }, { multi: true });
  • update is deprecated
  • use await for db operation
  • if you want to add new filed in collection ,first check it is added in Model or not(if you don't wan't use that filed as mandatory make is as "required: false")


Article.findByIdAndUpdate(id, { $set: { day: 'example' }}, { new: true }, function (err, article) {  if (err) return handleError(err);  res.send(article);});

I prefer this way because it's contains a callback function.

reference and more info: http://mongoosejs.com/docs/documents.html