How to use existing field while updating in mongo-go-driver?

I tried to update a field by using a value of existing field in mongo-go-driver.
here is my code:

col.UpdateMany(context.TODO(),bson.M{“id”:123},bson.M{“$set”:bson.M{“PreviousRecord”:“$CurrentRecord”}})

document before update:

{
   id:123,
   PreviousRecord:"old",
   CurrentRecord:"new"
}

document after update:

{
   id:123,
   PreviousRecord:"$CurrentRecord",
   CurrentRecord:"new"
}

this cmd works in mongo shell, but not for mongo-go-driver

Hi @Ji_Hua,

I tried to reproduce your results from the shell, but I wasn’t successful. The script I ran for local testing is here. I was testing against a single node replicaset with server version 4.4.1. You mentioned your command works in the shell, so can you see if my script differs from the commands you ran at all? If you want to run the script I linked, you can download it, start up a cluster, and run mongo main.js.

– Divjot

Hi Divjot,
sorry, the code I provided is golang.
here is the mongo shell code:

db.Col.updateMany(
    {"id":"123","SnoozedExpiryTime":{"$lte": ISODate('2020-12-15T20:00:00.000+00:00')}},
    [
        {"$set": {"PreviousRecord": "$CurrentRecord", "CurrentRecord":"new"}}
    ]
)

Thank you

I have got the same problem with golang driver. My mongo is 4.4. Is there any workarounds?

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