Updating the database is another essential operation that every database system must have. If you have put some data in your database and want to alter the values of any particular document, you need to use the update operation. In this chapter, you will learn about how to update data within MongoDB database.



Use of Update Operation in MongoDB

The update operation in a database is used to change or update values in a document. MongoDB makes use of the update() method for updating the documents within a MongoDB collection. For updating any specific documents, a new criterion can be incorporated with the update statement, which will only update the selected documents.

You have to put some specific condition in the form of the parameter to update the document in MongoDB. Here is a stepwise representation of how this can be performed:

  • Make use of the update() method.
  • Prefer the circumstance that you wish to implement for deciding which document needs an update in their database. Let us assume an example where you want to update your document which is having an id 4.
  • Then make use of the set command for modifying the Field Name.
  • Select which Field Name you wish for modifying and go into the new value consequently.

Syntax:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ]
   }
)

Example of the update() Method

First, let's select a record to update:

Example:

db.musicians.find({ _id: 4 }).pretty()

Output:


So, now let us update the list of instrument played by this person, by making use of the $set operator for updating a single field.

Example:

db.musicians.update(

{_id: 4},
{
  $set: { instrument: ["Vocals", "Violin", "Octapad"] }
}
)

Output:


Characteristics of Update in MongoDB

  • In case your field does not subsist in the current document, the $set operator will insert a new field with the specified value, until and unless it violates the type constraint.
  • MongoDB users can also make use of { multi: true } for updating multiple documents which will meet the query criteria.
  • Making use of { upsert: true } for creating a new document is also possible as no document goes with the query.

save() Method in Mongo

As you have encountered so far the update() method which helps in updating the Mongo database values in any existing document; on the other hand, the save() method is used to replace a document with another document conceded in the form of a parameter.

In other words, it can be said that the save() is a blend of both update() as well as insert(). As the save() method is used, the document that exists will get updated. Otherwise, when it does not exist, it will create one. When an _id field is not specified, MongoDB automatically creates a document with this _id containing an ObjectId value (as conducted by the insert() method).

Example:

db.musicians.save({
  "_id": 4,
  "name": "Steven Morse",
  "instrument": "Violin",
  "born": 1954
})

Output:



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram