1

I have the following data in my mongo database.

{
  "FirstName": "Sunil",
  "LastName": "Jamkatel"
}

I would like to add fullname to all the documents in the database such that it takes the firstname and lastname from a document and adds them up like:

{
  "FirstName": "Sunil",
  "LastName": "Jamkatel",
  "FullName": "Sunil Jamkatel"
}

Please let me know the ways to do it using mongoosejs. Thanks

Sunil Jamkatel
  • 133
  • 1
  • 12
  • Have you considered using a [virtual property](http://mongoosejs.com/docs/guide.html#virtuals) to compute `FullName` dynamically? – JohnnyHK Aug 14 '17 at 04:19

1 Answers1

1

One simple solution is to use a update with multi option set to true. The statement may look like:

YourModel.update(
  {},  
  {
    $set: {
      "FullName": "Sunil Jamkatel"
    }
  },
  {
    multi: true
  }
);
notme
  • 424
  • 5
  • 14