Undo enableSharding()

How do you undo sharding on the database after it has been enabled?

For completeness of the discussion the following is the answer I posted on the MongoDB university forum.

Just to clarify, sharding is done at the collection level rather than the database level.

I am not aware of any command to undo the sharding of a collection. But you can easily copy the shared data into a new collection. Then drop the original collection, so all indexes and sharding information will be gone. You may then move back all the data under the original name.

Hello @Altiano_Gerung,

The sharded cluster’s main components are the shards, mongos and the config server. Have you set up any of these? After that, there is a command to enable sharding on a database: sh.enableSharding("<database name>"). Then you can shard any of the collections in that database.

Sharding is enabled at database level, but actually you shard collections individually - that means a sharded database can have both sharded and unsharded collections. Once sharding is enabled on a database or a collection there is no disable sharding command - it is in general a difficult process. So, what is it you have so far?

You have a database which is sharding enabled. Are there any collections in it? Are any of the collections sharded? Is there any data in the collections?

Please share some more details to understand.

You may be interested in this post:

2 Likes

Thanks for the clarification.

thank you for the answer,

I simply just want to know if such thing is possible or not.

Well, I don’t know about the technical details that make undoing to be that difficult,

But let’s say I don’t want to use sharded cluster anymore,
(I.e. don’t want to have my collection sharded)

In general, what is the best approach to migrate my data?

You can make a backup of the sharded collection data (or use a backup you already have) to create a new collection without sharding. After creating the new collection and its data, you can discard the collection which is sharded.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.