MongoDB: enable sharding automatically

mongodbsharding

If we add any new data/collection to the existing(running) shard cluster. Is there any way that the data/collection gets sharded automatically.
If yes, How to achieve that?.

Best Answer

No it is not possible to automatically shard the collection. This is what you have to do per collection.

Ref:

You will need to run shardCollection command in order to shard a new collection. The collection must have an index whose prefix is the shard key. The following operation enables sharding for the people collection in the records database and uses the zipcode field as the shard key:

use admin
db.runCommand( { shardCollection: "records.people", key: { zipcode: 1 } } )

Shards a collection to distribute its documents across shards. You must run enableSharding on a database before running the shardCollection command. The shardCollection command must be run against the admin database.

A database can have a mixture of sharded and unsharded collections. Sharded collections are partitioned and distributed across the shards in the cluster. Unsharded collections are stored on a primary shard. Each database has its own primary shard.