283 — WouldChangeOwningShard

shardingwrite

An update would move the document to a different shard

What is happening?

WouldChangeOwningShard is returned when an update modifies the shard key value in a way that would move the document from one shard to another. Prior to MongoDB 5.0, changing the shard key value was not allowed. From 5.0 onward, the operation is allowed inside a retryable write or transaction but requires the document to be deleted from the source shard and re-inserted on the target shard.

Common causes

  1. Updating a field that is part of the shard key
  2. Upsert creating a document with a different shard key range than the filter
  3. Application updating a shard key field without using a transaction (pre-5.0)
  4. Aggregation $merge or $out writing documents to a different shard

Source code references

ErrorCodes::WouldChangeOwningShard found in 18 locations in the MongoDB server source.

Diagnostic Steps

Related Issues