In a sharded cluster, movePrimary reassigns the primary shard which holds all un-sharded collections in the database. movePrimary first 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:

db.runCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } )

For example, the following command moves the primary shard from test to shard0001:

db.runCommand( { movePrimary : "test", to : "shard0001" } )

When the command returns, the database’s primary shard location has switched to the specified shard. To fully decommission a shard, use the removeShard command.


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.

movePrimary is an administrative command that is only available for mongos instances.



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.


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.

←   moveChunk isdbgrid  →