How to update shard key

Dear all,
mongo 5.0.4 shard cluster, how to update shard key.
I am searching in google several days, i still do not get the answer.

first> i try to use : db.table01.updateOne({id:1},{$set:{id:8888}}) (id is my hashed shard key)
i get the error:Must run update to shard key field in a multi-statement transaction or with retryWrites: true."

then> try :db.table01.update({id:1},{$set:{id:8888}},{multi:true})
get the error:Multi-update operations are not allowed when updating the shard key field

then>I login mongos using mongo --retrywrites true, try : db.table01.updateOne({id:1},{$set:{id:8888}})
i get the error:Update operation was converted into a distributed transaction because the document being updated would move shards and that transaction failed. :: caused by :: Transaction was aborted :: caused by :: from shard rs_shard03 :: caused by :: ‘prepareTransaction’ is not supported for replica sets with arbiters"

i read the document , update shard key should in a transaction or retrywrites:true.
it seems that login mongs using --retrywrites true does not work.
so i try to use a transaction:
var session = db.getMongo().startSession()
var tb1 = session.getDatabase(‘testdb01’).getCollection(‘table01’)
i get the same error:
‘prepareTransaction’ is not supported for replica sets with arbiters",

Hi @Yi_deng,

I can see you have solved your issue in the following post :slight_smile:

Please note, as per the documentation, that transactions whose write operations span multiple shards will error and abort if any transaction operation reads from or writes to a shard that contains an arbiter.

As the error message “‘prepareTransaction’ is not supported for replica sets with arbiters” indicates, you need to replace the arbiter with a data-bearing secondary in order to use transactions or modify shard keys (which uses transactions).

Arbiters are not recommended for MongoDB deployments (see Replica set with 3 DB Nodes and 1 Arbiter - #8 by Stennie for related discussion).

I will post your resolution here:

i have configed a new cluster with no arbiter member, and i can update shard key successfully.


1 Like

jason, thanks for you kindly help.

1 Like