Docs Menu
Docs Home
/
MongoDB Manual
/ / /

sh.unshardCollection

On this page

  • Definition
  • Syntax
  • Compatibility
  • Considerations
  • Requirements
  • Examples
  • Learn More
sh.unshardCollection( namespace, shardID )

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 and the shard key is removed.

New in version 8.0.

Important

mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the unshardCollection command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

The sh.unshardCollection method requires you to specify the shard to receive the collection data. With the unshardCollection command, if you don't specify the destination shard, the cluster automatically selects the shard with the least data.

Note

Unsharding a collection is a write-intensive operation that can result in an increased 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.

sh.unshardCollection has the following syntax:

sh.unshardCollection( namespace, shardID )
Parameter
Type
Description
namespace
string
Specifies the database and collection to unshard.
shardID
string
Specifies the recipient shard ID. As MongoDB unshards the collection, it moves the collection data from their current shards to this specific shard.

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

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

Important

This command cannot be run on shared or serverless instances. For more information, see Unsupported Commands.

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

This example unshards a collection named inventory on the app database to the shard02 shard.

sh.unshardCollection( "app.inventory", "shard02" )

To get a list of the available shard IDs, run sh.status(). For details, see sh.status() Output.

Back

sh.stopBalancer