15 / 15

Is it possible to change a document's shard key value and how?

Difficulty: 6/10
shard key immutability, chunk migration, document rewrite

You can update a document's shard key value unless the shard key field is the immutable _id field.

  1. 1

    db.collection.replaceOne()

  2. 2

    db.collection.updateOne()

  3. 3

    db.collection.findOneAndReplace()

  4. 4

    db.collection.findOneAndUpdate()

  5. 5

    db.collection.findAndModify()

When updating the shard key value
  1. 1

    You must be on a mongos. Do not issue the operation directly on the shard.

  2. 2

    You must run either in a transaction or as a retryable write.

  3. 3

    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.

Scenario Questions

0-2 years experience

  1. 1You have a collection sharded on 'userId' and need to change the userId for one document. How would you handle that?
  2. 2What happens if you try to run an update that modifies the shard key field of a document in a sharded collection?
  3. 3If a document must move to a different shard because its shard key changes, what steps would you take in MongoDB?

2-5 years experience

  1. 1Our service lets users change their email address, which is part of the shard key. Explain how you would implement this change without downtime and discuss any performance implications.
  2. 2During a migration we saw that updating the shard key caused chunk migrations to stall. Walk me through how you would debug and resolve the issue.
  3. 3We considered a two‑phase update to change the shard key. What are the pros and cons of that versus a delete‑and‑re‑insert approach?

5-8 years experience

  1. 1Design a strategy to support mutable shard keys for a high‑traffic collection, ensuring minimal impact on read/write latency and balanced distribution.
  2. 2Explain how you would safely change the shard key for an existing collection with billions of documents, covering data migration, index rebuilding, and client compatibility.
  3. 3What are the risks and mitigation techniques when using the 'moveChunk' command as part of a bulk shard key change across a cluster?

8+ years experience

  1. 1Our platform stores multi‑tenant data with a shard key that includes tenantId, but we now need to allow tenantId changes. Propose an architectural migration plan that minimizes service disruption and supports future schema evolution.
  2. 2Discuss the long‑term maintenance implications of allowing mutable shard keys in a microservices environment, including versioning, backward compatibility, and operational tooling.
  3. 3If you were to redesign the data model to avoid mutable shard keys altogether, what alternatives would you consider and how would you evaluate their trade‑offs at scale?

Follow-up Questions

  • What exact MongoDB commands would you use for the rewrite?
  • How does this affect existing indexes and the balancer?
  • What metrics would you watch during the migration?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.