Update & Remove Operations in MongoDB

1. Update Operation

MongoDB’s update() and save() methods are used in updating a document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.

Syntax:

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

1.1 Examples

1.1.1 Updating the whole document

The following code snipet shows how to update the whole document in MongoDB:

db.inventory.update(
  {
       item: "MNO2" 
  },
 {
   $set: 
   {
       category: "apparel",
       details: { model: "14Q3", manufacturer: "XYZ Company" }
   },
  $currentDate: 
   { 
       lastModified: true
   }
 }
)

1.1.2 Updating a particular field of document

The following code snipet shows how to update a particular field in MongoDB:

db.inventory.update(
  { 
      item: "ABC1"
  },
  {
    $set:
    { 
         "details.model": "14Q2" 
    }
  }
)

1.1.3 Result flags of update operation

The following code snipet shows the use of flags in MongoDB:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

where,
nMatched: Number of documents matched
nUpserted: Number of documents upserted
nModified: Number of documents modified

1.1.4 Updating multiple documents

Add the following code as third argument to update() function:
> { multi: true }
Example:

db.inventory.update(
  { 
       category: "clothing"
  },
      {
          $set: 
           { 
               category: "apparel" 
           },
          $currentDate: 
           { 
               lastModified: true 
           }
      },
      { 
          multi: true 
      }
 )

1.1.5 UpsertOption: updating or inserting if does not exist

Add the following code as third argument to the function:
> {upsert:true}
Example:

db.inventory.update(
  { 
      item: "TBD1" 
  },
  {
      item: "TBD1", 
      details: 
       { 
           "model" : "14Q4", "manufacturer" : "ABC Company" 
       },
      stock: [ 
               {
                   "size" : "S", "qty" : 25 
               } 
             ],
      category: "houseware"
  },
  { 
      upsert: true 
  }
)

2. Remove Operation

MongoDB’s remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag.

  • deletion criteria − (Optional) deletion criteria according to documents will be removed.
  • justOne − (Optional) if set to true or 1, then remove only one document.

Syntax:

>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

2.1 Examples of Remove Operation

2.1.1 Remove all documents

The following example shows how to remove all documents from MongoDB:

db.inventory.remove({});

2.1.2 Remove documents that match condition

The following example shows how to remove documents that matches a given condition from MongoDB:

db.inventory.remove( { type : "food" } )

2.1.3 Remove single document that match a condition

The following example shows how to remove single document that matches a condition from MongoDB:

db.inventory.remove( { type : "food" }, 1 )

The CRUD Concepts of MongoDB can be referred by clicking here

3. Bulk Write Operations

MongoDB gives users the ability to perform write operations in bulk. Bulk write operations affect a single collection.
Syntax:

db.collection.bulkWrite()

This method provides the ability to perform bulk insert, update, and remove operations.
Bulk operations can be of two types:

  • 1.Ordered: When the operations have an ordered list, MongoDB executes the operations serially.While processing, if an error occurs on one of the write operations, MongoDB will return without processing, any remaining write operations in the list.

  • 2.Un-ordered: When the operations have an unordered list, operations can be executed in parallel, but this behavior is not guaranteed. Even if an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.

bulkWrite() supports the following methods:

MongoDB supports bulk insert as well.

Syntax:

db.collection.insertMany()