Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Are you looking to migrate an existing MongoDB application or use MongoDB Query Language (MQL) features? Consider Azure DocumentDB.
Are you looking for a database solution for high-scale scenarios with a 99.999% availability service level agreement (SLA), instant autoscale, and automatic failover across multiple regions? Consider Azure Cosmos DB for NoSQL.
One of the most basic operations is updating data in a collection. This article covers everything you need to know about updating data by using the Mongo Shell (Mongosh).
Using updateOne()
The updateOne() method updates the first document that matches a specified filter. The method takes two parameters:
filter: A document that specifies the criteria for the update. Use the filter to match the documents in the collection that you want to update. The filter document must be a valid query document.update: A document that specifies the update operations to perform on the matching documents. The update document must be a valid update document.
db.collection.updateOne(
<filter>,
<update>
)
For example, to update the name of a customer with _id equal to 1, use the following command:
db.customers.updateOne(
{ _id: 1 },
{ $set: { name: "Jane Smith" } }
)
In the preceding example, db.customers is the collection name, { _id: 1 } is the filter that matches the first document with _id equal to 1, and { $set: { name: "Jane Smith" } } is the update operation that sets the name field of the matched document to "Jane Smith".
You can also use other update operators like $inc, $mul, $rename, and $unset to update the data.
Using updateMany()
The updateMany() method updates all documents that match a specified filter. The method takes two parameters:
filter: A document that specifies the criteria for the update. Use the filter to match the documents in the collection that you want to update. The filter document must be a valid query document.update: A document that specifies the update operations to perform on the matching documents. The update document must be a valid update document.
db.collection.updateMany(
<filter>,
<update>
)
For example, to update the name of all customers that live in "New York", you can use the following command:
db.customers.updateMany(
{ city: "New York" },
{ $set: { name: "Jane Smith" } }
)
In this example, db.customers is the collection name, { city: "New York" } is the filter that matches all the documents that have a city field equal to "New York", and { $set: { name: "Jane Smith" } } is the update operation that sets the name field of all the matched documents to "Jane Smith".
You can also use other update operators like $inc, $mul, $rename, and $unset to update the data.
Next steps
- Learn how to use Studio 3T with Azure Cosmos DB for MongoDB.
- Learn how to use Robo 3T with Azure Cosmos DB for MongoDB.
- Explore MongoDB samples with Azure Cosmos DB for MongoDB.
- Trying to do capacity planning for a migration to Azure Cosmos DB? You can use information about your existing database cluster for capacity planning.
- If all you know is the number of vCores and servers in your existing database cluster, read about estimating request units using vCores or vCPUs.
- If you know typical request rates for your current database workload, read about estimating request units using Azure Cosmos DB capacity planner.