MongoDB How to update multiple documents with a single command?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MongoDB is a powerful NoSQL database that allows for flexible and dynamic data storage. One of its key features is the ability to update multiple documents with a single command, which is particularly useful when you want to apply the same change to multiple documents within a collection. In this article, we will explore how MongoDB handles such updates, provide technical explanations, examples, and enhance the discussion with additional details.
Updating Multiple Documents in MongoDB
Understanding MongoDB’s Update Operations
MongoDB provides several methods for updating documents:
updateOne(): Updates the first document that matches a specified filter.updateMany(): Updates all documents that match the specified filter criteria.replaceOne(): Replaces the first document that matches a filter with a new document.findOneAndUpdate(): Finds a single document, then updates it and returns the original or updated document.
For updating multiple documents at once, updateMany() is the function of choice.
The updateMany() Method Explained
updateMany() is a powerful method that allows you to modify all documents that match a given query. The syntax comprises two main parts:
<filter>: Specifies the selection criteria for which documents should be updated.<update>: Defines the modifications to apply to the matching documents.upsert: If true, performs an insert if no documents match the filter criteria.arrayFilters: Specifies which array elements to modify for an update operation on an array field.
Example Use Case
Consider a scenario where a company database holds information about employees, and you need to standardize the job titles. Here’s an example of how you might do this with MongoDB:
You want to update all documents where the job title is "Software dev" to "Software Developer". The command would look like this:
Key Considerations
When using updateMany(), there are a few considerations to keep in mind:
- Atomicity: Each
updateMany()operation is atomic at the document level. However, the operation as a whole is not atomic across multiple documents. - Performance: Updating multiple documents at once can be more efficient than updating them individually, but still requires reviewing the query plan according to your MongoDB version and indexing strategy.
The Upsert Option
The upsert option is particularly useful when you want to insert a document if none matches the query filter. For example:
This command will insert a new document with the jobTitle "Data Scientist" if none already exists.
Updating Arrays with arrayFilters
MongoDB's updateMany() is flexible enough to handle arrays. Suppose you have a document structure like this:
To update the status of all projects named "Project X", use arrayFilters:
This update statement efficiently handles array elements based on specific conditions.
Summary Table
| Method | Purpose | Atomic Across All Docs | Supports upsert |
updateOne() | Updates a single document | Yes | Yes |
updateMany() | Updates all matching documents | No | Yes |
replaceOne() | Replaces a single document completely | Yes | Yes |
findOneAndUpdate() | Finds, updates, and returns a document | Yes | No |
Conclusion
Updating multiple documents with MongoDB’s updateMany() provides flexibility and efficiency for operations that require modifications to several documents. By understanding the syntax, options, and considerations, developers can effectively manage data updates in MongoDB. Whether it's simple attribute changes or complex array manipulations, updateMany() handles the task efficiently, adhering to MongoDB's strengths in handling unstructured and semi-structured data.
By leveraging these features, MongoDB enables robust and scalable applications that meet a wide array of data management needs in today's fast-evolving digital landscape.
Related reading

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.