Docs Menu

Docs HomeGo

Update Multiple Documents

You can update multiple documents in a collection by using the UpdateMany() method.

Tip

Read the Usage Examples to learn how to run this example.

The following example performs the following on the listingsAndReviews collection:

  • Matches documents in which the market field of the address subdocument, address.market is "Sydney"

  • Updates the price in the matched documents by 1.15 times

coll := client.Database("sample_airbnb").Collection("listingsAndReviews")
filter := bson.D{{"address.market", "Sydney"}}
update := bson.D{{"$mul", bson.D{{"price", 1.15}}}}
result, err := coll.UpdateMany(context.TODO(), filter, update)
if err != nil {
panic(err)
}

View a fully runnable example.

After you run the full example, you can find the following updated documents in the listingsAndReviews collection:

// results truncated
...
{ "_id" : "10091713", ... , "name" : "Surry Hills Studio", ... , "price" : 181.00, ... },
{ "_id" : "9908871", ... , "name" : "Family friendly beach house", ... , "price" : 751.00, ... },
{ "_id" : "20989061", ... , "name" : "Big and sunny Narraben room", ... , "price" : 60.00, ... },
...

For an example on how to find multiple documents, see Find Multiple Documents.

To learn more about replacing documents, specifying query filters, and handling potential errors, see Modify Documents.

To learn more about update operators, see the MongoDB update operator reference documentation.

UpdateMany()

←  Update a DocumentReplace a Document →