MongoDB Update Documents

MongoDB provides following 4 methods to documents in a collection.

db.collection.updateOne(): Updates at most a single document that match a specified filter even though multiple documents may match the specified filter.
db.collection.updateMany(): Update all documents that match a specified filter.
db.collection.update(): Replaces at most a single document that match a specified filter even though multiple documents may match the specified filter.
db.collection.replaceOne(): Either updates or replaces a single document that match a specified filter or updates all documents that match a specified filter.

The other methods to update documents in MongoDB are:
db.collection.findOneAndReplace()
db.collection.findOneAndUpdate()
db.collection.findAndModify()
db.collection.save()
db.collection.bulkWrite()

MongoDB updateOne() Syntax:

Updates a single document within the collection based on the filter.

db.collection.updateOne( , , { upsert: , writeConcern: , collation: , arrayFilters: [ , ... ], hint: // Available starting in MongoDB 4.2.1 } )

MongoDB updateOne() Examples:

Assume collection restaurant contains following documents.

{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" }, { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 }, { "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 }

1. Update violations to 5 where name is ‘Central Perk Cafe’

db.restaurant.updateOne( { "name" : "Central Perk Cafe" }, { $set: { "violations" : 3 } } );

2. Update with Upsert: What this will do is if there is no document then updateOne will insert a
document.

In the below example name does not contains value ‘Pizza Rat’s Pizzaria’, so upsert will add a document to collection.

db.restaurant.updateOne( { "name" : "Pizza Rat's Pizzaria" }, { $set: {"_id" : 4, "violations" : 7, "borough" : "Manhattan" } }, { upsert: true } );

MongoDB updateMany() Syntax:

Updates all documents that match the specified filter for a collection.

db.collection.updateMany( , , { upsert: , writeConcern: , collation: , arrayFilters: [ , ... ], hint: // Available starting in MongoDB 4.2.1 } )

MongoDB updateMany() Examples:

1. Update all documents where violations are greater than 4 and $set a flag for review in restaurant collection.

db.restaurant.updateMany( { violations: { $gt: 4 } }, { $set: { "Review" : true } } )

Now document contains following documents:

{ "_id" : 1, "name" : "Central Perk Cafe", "Borough" : "Manhattan" } { "_id" : 2, "name" : "Rock A Feller Bar and Grill", "Borough" : "Queens", "violations" : 2 } { "_id" : 3, "name" : "Empire State Pub", "Borough" : "Brooklyn", "violations" : 0 } { "_id" : 4, "name" : "Pizza Rat's Pizzaria", "borough" : "Manhattan", "violations" : 7, "Review" : true }

MongoDB update() Syntax:

  • Modifies an existing document or documents in a collection. The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter.
  • By default, the db.collection.update() method updates a single document. Include the option multi: true to update all documents that match the query criteria.

db.collection.update( , , { upsert: , multi: , writeConcern: , collation: , arrayFilters: [ , ... ], hint: // Available starting in MongoDB 4.2 } )

MongoDB update() Examples:

For this example, create a collection student with following syntax.

db.students.insertMany([ { "_id" : 1, "grades" : [ 95, 92, 90 ] }, { "_id" : 2, "grades" : [ 98, 100, 102 ] }, { "_id" : 3, "grades" : [ 95, 110, 100 ] } ])

1. To update all elements that are greater than or equal to 100 in the grades array.

db.students.update( { grades: { $gte: 100 } }, { $set: { "grades.$[element]" : 100 } }, { multi: true, arrayFilters: [ { "element": { $gte: 100 } } ] } )

Collection contains following documents after above operation.

{ "_id" : 1, "grades" : [ 95, 92, 90 ] } { "_id" : 2, "grades" : [ 98, 100, 100 ] } { "_id" : 3, "grades" : [ 95, 100, 100 ] }

MongoDB replaceOne() Syntax:

Replaces a single document within the collection based on the filter.

, , { upsert: , writeConcern: , collation: , hint: })

MongoDB replaceOne() Examples:

1. Replaces single document where name: “Central Perk Cafe” with “name” : “Central Pork Cafe”

db.restaurant.replaceOne( { "name" : "Central Perk Cafe" }, { "name" : "Central Pork Cafe", "Borough" : "Manhattan" } );

2. replaceOne with upsert

db.restaurant.replaceOne( { "name" : "Pizza Rat's Pizzaria" }, { "_id": 4, "name" : "Pizza Rat's Pizzaria", "Borough" : "Manhattan", "violations" : 8 }, { upsert: true } )