> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# movePrimary (database command)

In a [sharded cluster](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-sharded-cluster), [`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary) reassigns the [primary shard](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-primary-shard) which holds all un-sharded collections in the database.  [`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary) first changes the primary shard in the cluster metadata, and then migrates all un-sharded collections to the specified [shard](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard). Use the command with the following form:

```javascript
db.adminCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } )
```

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

```javascript
db.adminCommand( { 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`](https://www.mongodb.com/docs/manual/reference/command/removeshard.md#mongodb-dbcommand-dbcmd.removeShard) command.

[`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary) is an administrative command that is only available for [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instances.

**Warning:**

After starting [`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary), do **not** perform any read or write operations against any unsharded collection in that database until the command completes. Read or write operations issued against those collections during the migration can result in unexpected behavior, including potential failure of the migration operation or loss of data.

## Compatibility

This command is available in deployments hosted in the following environments:

- [MongoDB Atlas](https://www.mongodb.com/docs/atlas): The fully managed service for MongoDB deployments in the cloud

* [MongoDB Enterprise](https://www.mongodb.com/docs/manual/administration/install-enterprise.md#std-label-install-mdb-enterprise): The subscription-based, self-managed version of MongoDB

* [MongoDB Community](https://www.mongodb.com/docs/manual/administration/install-community.md#std-label-install-mdb-community-edition): The source-available, free-to-use, and self-managed version of MongoDB

## Considerations

[`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) uses [`"majority"`](https://www.mongodb.com/docs/manual/reference/write-concern.md#mongodb-writeconcern-writeconcern.-majority-) write concern for [`movePrimary`.](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary)

### Unsharded Collections

Only run [`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary) after you move unsharded collections using [`sh.moveCollection()`](https://www.mongodb.com/docs/manual/reference/method/sh.movecollection.md#mongodb-method-sh.moveCollection). Starting in MongoDB 8.0, unmoved, unsharded collections are not available during the `movePrimary` process.

### MongoDB Search Indexes

If the database being moved contains unsharded collections that use MongoDB Search, their search indexes become unavailable after `movePrimary` completes. Because `movePrimary` updates the UUID (Universally unique identifier) for unsharded collections that you move, you must manually rebuild the MongoDB Search indexes on those collections.

### Maintenance Windows

[`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary) may require a significant time to complete depending on the size of the database and factors such as network health or machine resources. During migration, attempts to write or perform any DDL operations to the unsharded collections on the database being moved fail with the error: `"movePrimary is in progress"`.

Consider scheduling a maintenance window during which applications stop all reads and writes to the cluster. Issuing [`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary) during planned downtime mitigates the risk of encountering undefined behavior due to interleaving reads or writes to the unsharded collections in the database.

### Namespace Conflicts

[`movePrimary`](https://www.mongodb.com/docs/manual/reference/command/moveprimary.md#mongodb-dbcommand-dbcmd.movePrimary) fails if the destination shard contains a conflicting collection namespace. For example:

1. An administrator issues `movePrimary` to change the primary shard for the `hr` database.

2. A user or application issues a write operation against an unsharded collection in `hr` while `movePrimary` is moving that collection. The write operation creates the collection in the original primary shard.

3. An administrator later issues `movePrimary` to restore the original primary shard for the `hr` database.

4. `movePrimary` fails due to the conflicting namespace left behind from the interleaving write operation.

### Rebuilding Indexes

As part of the `movePrimary` operation, the destination shard must rebuild indexes on the migrated collections after becoming the primary shard. This may require a significant amount of time depending on the number of indexes per collection and the amount of data to index.

See [Index Builds on Populated Collections](https://www.mongodb.com/docs/manual/core/index-creation.md) for more information on the index build process.

### Change Streams

Starting in MongoDB 8.0, `movePrimary` doesn't [invalidate Event](https://www.mongodb.com/docs/manual/reference/change-events/invalidate.md#std-label-change-event-invalidate) collections that have [change streams](https://www.mongodb.com/docs/manual/changestreams.md#std-label-changeStreams). The change streams can continue to read events from collections after the collections are moved to a new shard.

In earlier MongoDB versions, `movePrimary` invalidates collection change streams and the change streams cannot read events from the collections.

In all MongoDB versions, `movePrimary` updates the UUID (Universally unique identifier) for moved unsharded collections.

## Additional Information

See [Remove Shards from a Sharded Cluster](https://www.mongodb.com/docs/manual/tutorial/remove-shards-from-cluster.md) for a complete procedure.
