You can update a document's shard key value unless the shard key field is the immutable _id field.
db.collection.replaceOne()
db.collection.updateOne()
db.collection.findOneAndReplace()
db.collection.findOneAndUpdate()
db.collection.findAndModify()
You must be on a mongos. Do not issue the operation directly on the shard.
You must run either in a transaction or as a retryable write.
You must include an equality condition on the full shard key in the query filter. For example, consider a messages collection that uses { activityid: 1, userid : 1 } as the shard key. To update the shard key value for a document, you must include activityid: <value>, userid: <value> in the query filter. You can include additional fields in the query as appropriate.