You can update multiple documents in a collection by using the UpdateMany()
method.
Example
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
// Updates documents that match a query filter by using the Go driver package main import ( "context" "fmt" "log" "os" "github.com/joho/godotenv" "go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/mongo" "go.mongodb.org/mongo-driver/v2/mongo/options" ) // Defines a Restaurant struct as a model for documents in the "restaurants" collection type Restaurant struct { ID bson.ObjectID `bson:"_id"` Name string `bson:"name"` Cuisine string `bson:"cuisine"` AverageRating float64 `bson:"avg_rating,omitempty"` } // Create a filter struct to specify the documents to update type UpdateManyRestaurantFilter struct { Cuisine string `bson:"cuisine"` Borough string `bson:"borough"` } // Defines a RestaurantUpdate struct to specify the fields to update type RestaurantUpdateMany struct { AverageRating float64 `bson:"avg_rating"` } func main() { if err := godotenv.Load(); err != nil { log.Println("No .env file found") } var uri string if uri = os.Getenv("MONGODB_URI"); uri == "" { log.Fatal("You must set your 'MONGODB_URI' environment variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/connect/mongoclient/#environment-variable") } client, err := mongo.Connect(options.Client().ApplyURI(uri)) if err != nil { panic(err) } defer func() { if err = client.Disconnect(context.TODO()); err != nil { panic(err) } }() coll := client.Database("sample_restaurants").Collection("restaurants") filter := UpdateManyRestaurantFilter{ Cuisine: "Pizza", Borough: "Brooklyn", } // Creates instructions to update the values of the "AverageRating" field update := bson.D{{"$set", RestaurantUpdateMany{AverageRating: 4.5}}} // Updates documents in which the value of the "Cuisine" // field is "Pizza" result, err := coll.UpdateMany(context.TODO(), filter, update) if err != nil { panic(err) } // Prints the number of updated documents fmt.Printf("Documents updated: %v\n", result.ModifiedCount) }
View a fully runnable example.
Expected Result
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
To learn more about replacing documents, specifying query filters, and handling potential errors, see Update Documents.
To learn more about update operators, see the MongoDB update operator reference documentation.