How to setup automatically with Sharding enabled. when app team creating new collection dynamically

Hi Team,

Can you please briefly explain with examples.

Able to have a new mongo collection created automatically with Sharding enabled.

When we dynamically create new collections how to auto sharding

Hi Team,

Any update please

Hi @hari_dba,

Your app team (or whoever is creating the collections) will have to script sharding the collections, as a shard key index needs to be defined for each collection. This can be done before or after the collections have data, but there are fewer exceptions to consider if you shard a collection before adding any data.

NOTE: depending on your use case, you may not want to shard every collection in a database. Sharding is typically done for distribution of data for workload and/or geo requirements. There may be no benefit for sharding small collections

Assuming you have previously Enabled sharding for a database, you can create a new sharded collection using sh.shardCollection(<namespace>,<key>).

If the collection doesn’t exist yet, sh.shardCollection() will create it.

If the collection already exists, you will have to create an index and ensure all documents have the field(s) required for your shard key index.

Regards,
Stennie

Hi Stennie,

Thanks for support.

I was understand please correct me without enable sharding for a database and sharding collection using sh.shadcollection(, ) data cannot distribution.
If you yes …

As you mentioned in Note:
" depending on your use case, you may not want to shard every collection in a database. Sharding is typically done for distribution of data for workload and/or geo requirements"

How it will be working please clarify with example…

Hi Team,

Do we have any automatically script sharding data like : Java, Json, C# etc…

Instead of below commands any script because when app team creating new DB and new collection by UI every time they need to help by DBA execute command instead of below command app team run script.

Enable sharding on the database:

  1. sh.enableSharding()

Create an index on field:
2) db.products.createIndex( { “field”:1 } )
Shard the products collection on field
3) sh.shardCollection( <databasename.collection>, { “filed”: “hashed” } )