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

# Unshard a Collection

You can unshard a sharded collection with the [`unshardCollection`](https://www.mongodb.com/docs/manual/reference/command/unshardcollection.md#mongodb-dbcommand-dbcmd.unshardCollection) command. 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.

By default, when you unshard a collection, MongoDB moves the collection's data to the shard with the least amount of data. Alternatively, you can specify which shard to place the data on.

## About this Task

### Compatibility

You can perform this task on deployments hosted in the following environments:

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

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

- `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.

## Access Control

If your deployment has [access control](https://www.mongodb.com/docs/manual/core/authorization.md#std-label-authorization) enabled, the [`enableSharding`](https://www.mongodb.com/docs/manual/reference/built-in-roles.md#mongodb-authrole-enableSharding) role grants you access to run the `unshardCollection` command.

## Before you Begin

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%.

## Steps

1. (Optional) List shard names

   If you want to put the data from your sharded collection on a specific shard, you need the target shard's name.

   To see the list of shard names in your cluster, use the [`listShards`](https://www.mongodb.com/docs/manual/reference/command/listshards.md#mongodb-dbcommand-dbcmd.listShards) command:

   ```javascript
   db.adminCommand( { listShards: 1 } )
   ```

   The `shards._id` field lists the name of each shard.

2. Unshard the collection

   To unshard a collection, run the [`unshardCollection`](https://www.mongodb.com/docs/manual/reference/command/unshardcollection.md#mongodb-dbcommand-dbcmd.unshardCollection) command. The following example unshards a collection called `us_accounts` in the `sales` database:

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

   After the unshard operation completes, the data in the `us_accounts` collection is on `shard1`. If you omit the `toShard` field, the data is placed on the shard with the least amount of data.

3. Confirm that the collection is unsharded

   To confirm that the collection is unsharded, use the [`$shardedDataDistribution`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/shardeddatadistribution.md#mongodb-pipeline-pipe.-shardedDataDistribution) stage and try to match on the unsharded namespace:

   ```javascript
   db.aggregate( [
      { $shardedDataDistribution: { } },
      { $match: {  "ns": "sales.us_accounts" } }
   ] )
   ```

   If the aggregation doesn't return any data, the collection is unsharded.

## Learn More

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

- [Remove Shards from a Sharded Cluster](https://www.mongodb.com/docs/manual/tutorial/remove-shards-from-cluster.md#std-label-remove-shards-from-cluster-tutorial)

- [Refine a Shard Key](https://www.mongodb.com/docs/manual/core/sharding-refine-a-shard-key.md#std-label-shard-key-refine)
