- Reference >
- Database Commands >
- Sharding Commands >
- movePrimary
movePrimary¶
On this page
-
movePrimary¶ In a sharded cluster,
movePrimaryreassigns the primary shard which holds all un-sharded collections in the database.movePrimaryfirst changes the primary shard in the cluster metadata, and then migrates all un-sharded collections to the specified shard. Use the command with the following form:For example, the following command moves the primary shard from
testtoshard0001:When the command returns, the database’s primary shard location has switched to the specified shard. To fully decommission a shard, use the
removeShardcommand.Note
The destination shard must rebuild its indexes in the foreground when it becomes the primary shard. The collection locks while the indexes are building, preventing reads and writes until the indexes are finished. See Index Creation for details.
movePrimaryis an administrative command that is only available formongosinstances.
Considerations¶
Behavior¶
Avoid accessing an un-sharded collection during migration.
movePrimary does not prevent reading and writing during its
operation, and such actions yield undefined behavior.
movePrimary may take significant time to complete, and
you should plan for this unavailability.
movePrimary will fail if the destination shard contains a
conflicting collection name. This may occur if documents are written to an
un-sharded collection while the collection is moved away, and later the original
primary shard is restored.
Use¶
If you use the movePrimary command to move un-sharded
collections, you must either restart all mongos instances,
or use the flushRouterConfig command on all
mongos instances before writing any data to the
cluster. This action notifies the mongos of the new shard
for the database.
If you do not update the mongos instances’ metadata cache
after using movePrimary, the mongos may not
write data to the correct shard. To recover, you must manually
intervene.
Additional Information¶
See Remove Shards from an Existing Sharded Cluster for a complete procedure.