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

# moveCollection (database command)

## Definition

**New in version 8.0**

Moves a single unsharded collection to a different [shard](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard). Run `moveCollection` with a [`mongos`](https://www.mongodb.com/docs/manual/reference/program/mongos.md#mongodb-binary-bin.mongos) instance while using the [admin database.](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-admin-database)

## 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

**Important:**

This command is not supported in M0 and Flex clusters. For more information, see [Unsupported Commands.](https://www.mongodb.com/docs/atlas/unsupported-commands/)

- [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

## Restrictions

You cannot use `moveCollection` for [Queryable Encryption](https://www.mongodb.com/docs/manual/core/queryable-encryption.md#std-label-qe-manual-feature-qe) collections.

## Syntax

The command has the following syntax:

```javascript
db.adminCommand(
  {
    moveCollection: "<database>.<collection>",
    toShard: "<ID of the recipient shard>",
  }
)
```

**Note:**

Use the [`listShards`](https://www.mongodb.com/docs/manual/reference/command/listshards.md#mongodb-dbcommand-dbcmd.listShards) command to retrieve the ID of the recipient shard.

## Command Fields

The command takes the following fields:

| Field | Type | Description |
| --- | --- | --- |
| `moveCollection` | string | Database and name of the collection to move. |
| `toShard` | string | ID of the recipient shard. |

## Considerations

- `moveCollection` can only be run on sharded clusters.

- `moveCollection` can only move unsharded collections.

- Only one `moveCollection` operation can run at a time. You cannot run concurrent `moveCollection` operations.

- Only one of the following operations can run at a time on the cluster: [`moveCollection`](https://www.mongodb.com/docs/manual/reference/command/movecollection.md#mongodb-dbcommand-dbcmd.moveCollection), [`reshardCollection`](https://www.mongodb.com/docs/manual/reference/command/reshardcollection.md#mongodb-dbcommand-dbcmd.reshardCollection), [`rewriteCollection`](https://www.mongodb.com/docs/manual/reference/command/rewritecollection.md#mongodb-dbcommand-dbcmd.rewriteCollection), and [`unshardCollection`.](https://www.mongodb.com/docs/manual/reference/command/unshardcollection.md#mongodb-dbcommand-dbcmd.unshardCollection)

- `moveCollection` has a 5 minute minimum duration.

- [`writeConcernMajorityJournalDefault`](https://www.mongodb.com/docs/manual/reference/replica-configuration.md#mongodb-rsconf-rsconf.writeConcernMajorityJournalDefault) must be `true`.

- To move a collection that has a [uniqueness](https://www.mongodb.com/docs/manual/core/index-unique.md#std-label-index-type-unique) constraint, the new index must satisfy the [unique index requirements](https://www.mongodb.com/docs/manual/core/sharding-shard-key-indexes.md#std-label-sharding-shard-key-unique) for any existing unique indexes.

- If the collection that is being moved uses MongoDB Search, its search indexes become unavailable after `moveCollection` completes. You must manually rebuild the search indexes.

- If the collection you're moving is archived in [Atlas Online Archives](https://www.mongodb.com/docs/atlas/online-archive/manage-online-archive.md#std-label-manage-online-archive), the online archive files are marked as `Orphaned` once the moving operation completes. You can [create](https://www.mongodb.com/docs/atlas/online-archive/configure-online-archive/) another online archive for the same database, collection, and fields as the orphaned archive as long as there is no other archive for that same combination in the `Active` state.

- You cannot make topology changes, such as add or remove shard or transition between embedded and dedicated config servers, until `moveCollection` completes.

- You cannot run the following operations against the collection that is being moved while `moveCollection` is in progress:

  - [`collMod`](https://www.mongodb.com/docs/manual/reference/command/collmod.md#mongodb-dbcommand-dbcmd.collMod)

  - [`convertToCapped`](https://www.mongodb.com/docs/manual/reference/command/converttocapped.md#mongodb-dbcommand-dbcmd.convertToCapped)

  - [`createIndexes`](https://www.mongodb.com/docs/manual/reference/command/createindexes.md#mongodb-dbcommand-dbcmd.createIndexes)

  - [`db.collection.createIndexes()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.createindexes.md#mongodb-method-db.collection.createIndexes)

  - [`drop`](https://www.mongodb.com/docs/manual/reference/command/drop.md#mongodb-dbcommand-dbcmd.drop)

  - [`db.collection.drop()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.drop.md#mongodb-method-db.collection.drop)

  - [`dropIndexes`](https://www.mongodb.com/docs/manual/reference/command/dropindexes.md#mongodb-dbcommand-dbcmd.dropIndexes)

  - [`db.collection.dropIndex()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.dropindex.md#mongodb-method-db.collection.dropIndex)

  - [`renameCollection`](https://www.mongodb.com/docs/manual/reference/command/renamecollection.md#mongodb-dbcommand-dbcmd.renameCollection)

  - [`db.collection.renameCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.renamecollection.md#mongodb-method-db.collection.renameCollection)

- You cannot run the following operations against the cluster while `moveCollection` is in progress:

  - [`addShard`](https://www.mongodb.com/docs/manual/reference/command/addshard.md#mongodb-dbcommand-dbcmd.addShard)

  - [`removeShard`](https://www.mongodb.com/docs/manual/reference/command/removeshard.md#mongodb-dbcommand-dbcmd.removeShard)

  - [`db.createCollection()`](https://www.mongodb.com/docs/manual/reference/method/db.createcollection.md#mongodb-method-db.createCollection)

  - [`dropDatabase`](https://www.mongodb.com/docs/manual/reference/command/dropdatabase.md#mongodb-dbcommand-dbcmd.dropDatabase)

  - [`transitionToDedicatedConfigServer`](https://www.mongodb.com/docs/manual/reference/command/transitiontodedicatedconfigserver.md#mongodb-dbcommand-dbcmd.transitionToDedicatedConfigServer)

  - [`transitionFromDedicatedConfigServer`](https://www.mongodb.com/docs/manual/reference/command/transitionfromdedicatedconfigserver.md#mongodb-dbcommand-dbcmd.transitionFromDedicatedConfigServer)

- Index builds that occur while `moveCollection` is in progress might silently fail.

  - Do not create indexes while `moveCollection` is in progress.

  - Do not call `moveCollection` if there are ongoing index builds.

## Requirements

Before you move your collection, ensure that you meet the following requirements:

- Your application can tolerate a period of **two seconds** where the affected collection blocks writes. During the time period where writes are blocked, your application experiences an increase in latency.

- Your database meets these resource requirements:

  - Ensure the shard you are moving the collection to has enough storage space for the collection and its indexes. The destination shard requires at least `( Collection storage size + Index Size ) * 2` bytes available.

  - Ensure that your I/O capacity is below 50%.

  - Ensure that your CPU load is below 80%.

**Important:**

These requirements are not enforced by the database. A failure to allocate enough resources can result in:

- the database running out of space and shutting down

- decreased performance

- the operation taking longer than expected

If your application has time periods with less traffic, perform this operation on the collection during that time if possible.

## Example

This example moves an unsharded collection named `inventory` on the `app` database to the `shard02` shard.

```javascript
db.adminCommand(
  {
    moveCollection: "app.inventory",
    toShard: "shard02"
  }
)
```

To get a list of the available shard IDs, run [`sh.status()`](https://www.mongodb.com/docs/manual/reference/method/sh.status.md#mongodb-method-sh.status). For details, see [sh.status() Output.](https://www.mongodb.com/docs/manual/reference/method/sh.status.md#std-label-sh-status-output-examples)

## Learn More

- [`sh.moveCollection()`](https://www.mongodb.com/docs/manual/reference/method/sh.movecollection.md#mongodb-method-sh.moveCollection)

- [Moveable Collections](https://www.mongodb.com/docs/manual/core/moveable-collections.md#std-label-moveable-collections)
