Docs Menu
Docs Home
/
MongoDB Manual
/ / /

unshardCollection

On this page

  • Definition
  • Compatibility
  • Restrictions
  • Syntax
  • Command Fields
  • Considerations
  • Requirements
  • Examples
  • Learn More
unshardCollection

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.

Tip

In mongosh, this command can also be run through the sh.unshardCollection().

Helper methods are convenient for 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.

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.

This command 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.

You cannot use unshardCollection for:

db.adminCommand( {
unshardCollection: "<database>.<collection>",
toShard: "<shard-id>"
} )
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.

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

The following example unshards the sales.eu_accounts collection:

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

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

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

Back

unsetSharding