How can we assign one field value to another field from a document?

here is the document names “seasonrank”:

{ 
    "_id" : ObjectId("655f1d9fb20a2c9cd6af7a38"), 
    "playerid" : "5e16a6aa-dea5-48ab-b0cf-805378d97d34", 
    "channel" : "android", 
    "logintime" : 1700043180, 
    "ranklevel" : 100, 
    "lastscore" : "0", 
    "rankname" : "", 
    "tasks" : [
        210007
    ], 
    "level" : 13, 
    "score" : 10, 
    "task" : [

    ], 
    "taskprocess" : {
        "count" : 0, 
        "getlist" : null
    }
}

When I run the command in MongoDB versions below 4.0
db.getCollection(‘seasonrank’).updateMany(
{},
[{“$set”: {“lastscore”: “$score”}}]
)
it will be ok ,however when this commands run at 4.0.2 or higher version will receieve an error:

{ 
    "ok" : 0.0, 
    "errmsg" : "BSON field 'update.updates.u' is the wrong type 'array', expected type 'object'", 
    "code" : 14.0, 
    "codeName" : "TypeMismatch"
}

how to solve, anyone who can help me ? regards!

Hi @gao_ke,

The command you provided seems to work for me on version 7.0.2.

Can you try the following using mongosh and advise the output?:

  1. From the client getting the error, run:
db.getCollection('seasonrank').updateMany(
{},
[{"$set": {"lastscore": "$score"}}]
)

Note: Please include / copy and paste the command being run as well

  1. From the same mongosh session, run:
db.version()

Just want to clarify that there isn’t another mongod instance that the client is being connected to.

Regards,
Jason