Update Multiple Documents![](/docs/drivers/go/v1.8/assets/link.svg)
You can update multiple documents in a collection by using the UpdateMany()
method.
Example![](/docs/drivers/go/v1.8/assets/link.svg)
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.
Expected Result![](/docs/drivers/go/v1.8/assets/link.svg)
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.
Additional Information![](/docs/drivers/go/v1.8/assets/link.svg)
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.