Search code examples
databasemongodbnosqlsharding

MongoDB database level sharding and not collection level sharding


Let's say I have 10 databases (1, 2, 3, ..., 9, 10) and a 2 shard MongoDB cluster.

I don't want to shard data at collection level but rather keep entire data of any given database on a given shard.

So I would like my MongoDB cluster to hold values like following (assume shard key to be odd even for simplicity) - Shard 0: DB(1, 3, 5, 7, 9) Shard 1: DB(2, 4, 6, 8, 10)

How do I achieve the same?


Solution

  • When you call enableSharding you can specify the primary shard for each database. Absent additional per-collection configuration, the data should be stored on those shards.

    movePrimary can be used to change a database's primary shard.