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

旧街凉风 提交于 2019-12-04 04:18:03
Sergey Zhukov

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.

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

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!