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

# unshardCollection (database command)

## Definition

Unshards an existing sharded collection and moves the collection data onto a single shard. When you unshard a collection, the collection cannot be partitioned across multiple [shards](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard) and the [shard key](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-shard-key) is removed.

**New in version 8.0**

**Tip:**

In [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#std-program-mongosh), this command can also be run through the [`sh.unshardCollection()`](https://www.mongodb.com/docs/manual/reference/method/sh.unshardcollection.md#mongodb-method-sh.unshardCollection).

Helper methods are convenient for [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#std-program-mongosh) users, but they may not return the same level of information as database commands.  In cases where the convenience is not needed or the additional return fields are required, use the database command.

This command must run on the `admin` database.

If the collection has a [zone configuration](https://www.mongodb.com/docs/manual/core/zone-sharding.md#std-label-zone-sharding) applied, you must first remove the range associations and shards from the zone before you unshard the collection. For more information, see [Unshard Zones.](https://www.mongodb.com/docs/manual/reference/command/unshardcollection.md#std-label-unshard-remove-zone-ranges)

**Note:**

Unsharding a collection is a write-intensive operation that can result in an increased [oplog](https://www.mongodb.com/docs/manual/reference/glossary.md#std-term-oplog) growth rate. To help mitigate this, consider the following configuration changes:

- To prevent unbounded oplog growth, set a fixed oplog size.

- To reduce the chance of secondaries becoming stale, increase the oplog size.

For more details, see the [Replica Set Oplog.](https://www.mongodb.com/docs/manual/core/replica-set-oplog.md#std-label-replica-set-oplog)

## Compatibility

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

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

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

**Note:**

This task is not available on the MongoDB Atlas Free or Flex Tiers.

## Restrictions

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

## Syntax

```javascript
db.adminCommand( {
   unshardCollection: "<database>.<collection>",
   toShard: "<shard-id>"
} )
```

## Command Fields

| Field | Type | Necessity | Description |
| --- | --- | --- | --- |
| `unshardCollection` | string | Required | Specifies the database and collection to unshard. |
| `toShard` | string | Optional | Specifies the recipient shard ID. As MongoDB unshards the collection, it moves collection data from their current shards to this specific shard. If unspecified, the cluster selects the shard with the least amount of data. |

## Considerations

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

- `unshardCollection` can only operate on sharded collections.

- Only one `unshardCollection` operation can run at a time. You cannot run concurrent `unshardCollection` 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)

- `unshardCollection` has a 5 minute minimum duration.

- You must rebuild MongoDB Search indexes after `unshardCollection` runs.

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

- You cannot make topology changes, such as adding or removing shards or transitioning between embedded and dedicated config servers, until `unshardCollection` completes.

- You cannot run the following operations on the collection that is being unsharded while `unshardCollection` 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 on the cluster while `unshardCollection` 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 `unshardCollection` is in progress might silently fail.

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

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

- To avoid error, MongoDB automatically drops the zones in your collection when you run `unshardCollection`.

- If the collection you're unsharding uses [MongoDB Search](https://www.mongodb.com/docs/atlas/atlas-search/), the search index becomes unavailable when the unsharding operation completes. You need to manually rebuild the search index once the unsharding operation completes.

- If the collection you're unsharding 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 unsharding 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.

## Requirements

Before you unshard 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%.

## Behavior

### Unshard Zones

To unshard a collection that uses [zone sharding](https://www.mongodb.com/docs/manual/core/zone-sharding.md#std-label-zone-sharding), you must first stop the balancer, then remove the range and shard from the zone.

## Examples

### Unshard a Collection

The following example unshards the `sales.eu_accounts` collection:

```javascript
db.adminCommand( {
    unshardCollection: "sales.eu_accounts"
} )
```

### Unshard to a Specific Shard

The following example unshards the `sales.us_accounts` collections and places the collection data on `shard1`:

```javascript
db.adminCommand( {
    unshardCollection: "sales.eu_accounts",
    toShard: "shard1"
} )
```

## Learn More

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

- [Unshard a Collection](https://www.mongodb.com/docs/manual/tutorial/unshard-collection.md#std-label-unshard-collection-task)
