Update Documents
Overview
In this guide, you can learn how to update documents in a MongoDB collection. Update operations specify the fields and values to change in one or more documents. They apply changes specified in an update document to one or more documents that match your query filter.
To learn how to updated embedded arrays or to update or insert in a single operation, see the following pages:
Update operations can modify fields and values:
The updateOne() method changes the first document your query filter matches and the
updateMany() method changes all the documents your query filter matches.
You can call the updateOne()
and updateMany()
methods on a
MongoCollection
instance as follows:
collection.updateOne(<query>, <updateDocument>); collection.updateMany(<query>, <updateDocument>);
Update Operation Parameters
The updateOne()
and updateMany()
methods both have the following
parameters:
query
specifies a query filter with the criteria to match documents to update in your collection.update
specifies the fields and values to modify in the matching document or documents. The examples in this section use the Updates Builders to create the update document.(Optional)
updateOptions
specifies options that you can set to customize how the driver performs the update operation. To learn more about this type, see the API documentation for UpdateOptions.
You can create the updateDocument
using an Updates
builder as
follows:
Bson update = Updates.operator(<field>, <value>);
To view a complete list of Updates builders and their usage, see Updates in the API documentation.
Examples
In the following examples, a paint store sells five different
colors of paint. The paint_inventory
collection represents their
current inventory:
{ "_id": 1, "color": "red", "qty": 5 } { "_id": 2, "color": "purple", "qty": 8 } { "_id": 3, "color": "yellow", "qty": 0 } { "_id": 4, "color": "green", "qty": 6 } { "_id": 5, "color": "pink", "qty": 0 }
Update One Example
The following example demonstrates how to change the value of the
color
field in the first matching document in which the value of
qty
is 0
:
Bson filter = Filters.eq("qty", 0); Bson update = Updates.set("color", "dandelion"); // Updates first matching document UpdateResult result = collection.updateOne(filter, update);
If multiple documents match the query filter specified in
the updateOne()
method, it updates the first result. You can
specify a sort in an UpdateOptions
instance to apply an order to
matched documents before the server performs the update operation, as
shown in the following code:
UpdateOptions options = UpdateOptions.sort(ascending("color")); UpdateResult result = collection.updateOne(filter, document, options);
Update Many Example
The paint store receives a fresh shipment and needs to update their inventory. The shipment contains 20 cans of each paint color.
To update the inventory, call the updateMany()
method specifying the
following:
Query filter that matches all the colors
Update document that contains instructions to increment the
qty
field by20
Bson filter = Filters.empty(); Bson update = Updates.inc("qty", 20); // Updates all documents and prints the number of matched and modified documents UpdateResult result = collection.updateMany(filter, update); System.out.println("Matched document count: " + result.getMatchedCount()); System.out.println("Modified document count: " + result.getModifiedCount());
The output of the preceding code resembles the following:
Matched document count: 5 Modified document count: 5
The following shows the updated documents in the paint_inventory
collection:
{ "_id": 1, "color": "red", "qty": 25 } { "_id": 2, "color": "purple", "qty": 28 } { "_id": 3, "color": "yellow", "qty": 20 } { "_id": 4, "color": "green", "qty": 26 } { "_id": 5, "color": "pink", "qty": 20 }
If zero documents match the query filter in the update operation,
updateMany()
makes no changes to documents in the collection. See
our upsert guide to
learn how to insert a new document instead of updating one if no
documents match.
Important
The updateOne()
and updateMany()
methods cannot make changes
to a document that violate unique index constraints on the
collection. For more information about constraints on unique indexes,
see Unique Indexes in the
MongoDB Server manual.