I am looking for a solution to the same issue.How do I do this below command via Mongo Golang Driver UpdateMany Option? Both propertyA and propertyB already exist in the db, and I am trying to copy the value from propertyA to propertyB.
db.<collection>.update({}, [{$set: {'propertyB': '$propertyA'}}], {"multi": true})
I tried multiple options in my go - code and none of them seem to work
update := bson.M{
“$set”: bson.M{
“propertyB”: “$propertyA”,
},
}
// Execute the update operation
result, err := collection.UpdateMany(ctx, bson.M{}, update)
This updated the document in the DB with value of propertyB =“$propertyA”, It ignored the $ that was passed to it.
Then I tried this:
update := bson.D{{“$set”, bson.D{{ “propertyB”, "$propertyA”}}}}
opts := options.Update().SetComment(“xxxx”))
res, err := m.db.Collection(collName).UpdateMany(ctx, bson.M{“_id: “myid”}},mongo.Pipeline{pipe}), opts)
But that gave me error like this
,“command”:{“q”:{“_id”:“66bec6582d35454c1f51cf27”},“u”:[{“$set”:[“propertyB”,“$propertyA”]}],“multi”:true,“upsert”:false,“comment”:“xxxx”},
“numYields":0,“ok”:0,“errMsg”:“$set specification stage must be an object, got array”,“errName”:“Location40272”,“errCode”:40272,“locks”:{“ParallelBatchWriterMode”:{“acquireCount”:{“r”:1}},“FeatureCompatibilityVersion”:{“acquireCount”:{“w”:1}},“ReplicationStateTransition”:{“acquireCount”:{“w”:1}},“Global”:{“acquireCount”:{“w”:1}},“Database”:{“acquireCount”:{“w”:1}},“Collection”:{“acquireCount”:{“w”:1}},“Mutex”:{“acquireCount”:{“r”:1}}},“flowControl”:{“acquireCount”:1,“timeAcquiringMicros”:111},“readConcern”:{“level”:“local”,“provenance”:“implicitDefault”},“remote”:“172.17.0.1:59242”,“durationMillis”:0}}
Any idea what I am doing wrong? Are there examples on how we can do via golang driver.
Driver Version: go.mongodb.org/mongo-driver v1.11.4
Mongo Version (running in docker) : mongo:5.0.22