Sunil Jamkatel
Sunil Jamkatel

Reputation: 133

Update all the data in mongodb using mongoose

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

Upvotes: 1

Views: 704

Answers (1)

notme
notme

Reputation: 444

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
  }
);

Upvotes: 1

Related Questions