Database
NoSQL
MongoDB
CRUD Operations
Update Document

MongoDB - Update Document

Introduction

Updating documents is a fundamental operation in MongoDB, allowing you to modify existing data within a collection. MongoDB provides several methods for handling updates, each suited to different scenarios. Understanding these methods and their functionalities is crucial for effective data management.

Update Methods

update()

The update() method was traditionally used to modify documents in MongoDB. It allowed updates based on a specified query and applied the given update operations. However, this method is deprecated in favor of more specific methods such as updateOne() and updateMany().

db.collection_name.update(
   { filter_criteria },
   { $set: { field_to_update: new_value } },
   { multi: true }
)

updateOne()

The updateOne() method updates a single document that matches the specified filter criteria. If multiple documents match the filter, only the first one found will be updated. This method is ideal when you need to update just one document that meets your criteria.

db.collection_name.updateOne(
   { "state": "Maharashtra" },
   { $set: { "capital": "Mumbai" } }
)

updateMany()

The updateMany() method updates all documents that match the specified filter criteria. It applies the update operations to every document that meets the criteria. Use this method when you want to apply the same changes to multiple documents simultaneously.

db.collection_name.updateMany(
   { "country": "India" },
   { $set: { "continent": "Asia" } }
)

replaceOne()

The replaceOne() method completely replaces a document that matches the filter with a new document. Unlike updateOne(), which only modifies specified fields, replaceOne() substitutes the entire document. This method is useful when you need to replace an existing document entirely with a new one.

db.collection_name.replaceOne(
   { "name": "Old City" },
   { "name": "New City", "population": 1000000, "state": "New State" }
)

Syntax

The general syntax for updating documents in MongoDB includes specifying the collection name, the query to select documents, the update operations, and optional parameters that control the update behavior.

Important Considerations

Upsert Option: upsert A combination of "update" and "insert." It ensures that if a document matching the filter criteria is found, it will be updated. If no matching document is found, a new document will be created with the provided data.

db.collection_name.updateOne(
   { "name": "Nonexistent City" },
   { $set: { "population": 500000 } },
   { upsert: true }
)

Handling Multiple Matches: With updateMany(), all documents that match the filter will be updated. For updateOne(), only the first matching document will be modified.

Atomic Operations: MongoDB supports atomic operations on a single document. This ensures that updates are applied reliably even if multiple processes access the same document concurrently.

Field Updates: When updating documents, ensure that your update operations are carefully crafted to avoid unintended changes. MongoDB supports various update operators like $set, $unset, and $inc to modify specific fields.

  • $set: Sets the value of a field.
db.collection_name.updateOne(
   { "name": "City" },
   { $set: { "population": 2000000 } }
)
  • $unset: Removes a field.
db.collection_name.updateOne(
   { "name": "City" },
   { $unset: { "old_field": "" } }
)
  • $inc: Increments the value of a field.
db.collection_name.updateOne(
   { "name": "City" },
   { $inc: { "population": 50000 } }
)

Conclusion

MongoDB provides several methods for updating documents, each designed for different use cases. Whether you need to update a single document, multiple documents, or replace a document entirely, understanding these methods and their syntax helps in effectively managing and modifying your data.