Choosing a shard key

you can shard a collection on _id using hashed sharding. In my honest opinion, that is the easiest way to shard a collection and it will distribute your data across shards and get you additional performance and storage. However, as @Prasad_Saya stated, you need to build the sharded cluster which involves additional hardware and configuration.

Keep in my mind the below two caveats with sharding:-

  • Once you shard a collection, the selection of the shard key is immutable; i.e. you cannot select a different shard key for that collection.
  • Starting in MongoDB 4.2, you can update a document’s shard key value unless the shard key field is the immutable _id field. For details on updating the shard key, see Change a Document’s Shard Key Value.Before MongoDB 4.2, a document’s shard key field value is immutable.

More details about shard keys can be found here.

1 Like