15 / 15

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

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.

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

Scenario Questions

0-2 years experience
  1. 1

    You have a collection sharded on 'userId' and need to change the userId for one document. How would you handle that?

  2. 2

    What happens if you try to run an update that modifies the shard key field of a document in a sharded collection?

  3. 3

    If 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. 1

    Our 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. 2

    During 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. 3

    We 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. 1

    Design a strategy to support mutable shard keys for a high‑traffic collection, ensuring minimal impact on read/write latency and balanced distribution.

  2. 2

    Explain 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. 3

    What 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. 1

    Our 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. 2

    Discuss the long‑term maintenance implications of allowing mutable shard keys in a microservices environment, including versioning, backward compatibility, and operational tooling.

  3. 3

    If 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?