While reading the update many records link. There is mongodb shell query which is used to update many records in the db. Then I want to implement below given query in the golang for update many documents. I also check the mgo document There is a function which update many documents But it will take only two parameters first is selector
and other one is the update
. But in the mongodb link there are three parameters we have to pass in the query. In the collection there are 200k records and I have only update the 2000 records. I want to implement the below query in the golang:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
I used it like this:
UpdateAll(config.BookingsCollection, nil, bson.M{"$set": bson.M{"is_price_field": 1}}).Limit(2000)
It will gives me the error:
multiple-value UpdateAll() in single-value context
When I remove the Limit(2000) then it will show no error but it will not satisfy my result.
Updated
Basically I'm following the MVC structure
Controllers:
func BulkUpdateBookings(c *gin.Context) {
err := models.UpdateBulkBookingsDb()
fmt.Println(err)
}
Models:
func UpdateBulkBookingsDb() (err error) {
mongoSession := config.ConnectDb()// database connection
defer mongoSession.Close()
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
getCollection := sessionCopy.DB(Database).C(collection)
_, err = getCollection.UpdateAll(nil, bson.M{"$set": bson.M{"is_price_update": 0}})
return err
}