Introducing Dragonfly Cloud! Learn More

Question: How do you find the shard key in MongoDB?

Answer

Finding the shard key used in a sharded MongoDB collection is crucial for understanding how data is distributed across shards. The shard key determines how the data is partitioned and can impact query performance significantly. Here's how to find it:

Query the Collection's Metadata

To find out what shard key has been set for a specific collection, you can query the config.collections collection in the config database. This collection stores metadata about all the collections in the sharded cluster, including their shard keys.

use config db.collections.find({ _id: '<yourDatabaseName>.<yourCollectionName>' }, { key: 1 })

Replace <yourDatabaseName> with the name of your database and <yourCollectionName> with the name of your collection. This query will return the shard key as part of the document.

Using the sh.status() Command

Another way to check the shard key (and get an overview of your sharded environment) is by using the sh.status() command. This command provides detailed information about the sharded cluster, including databases, collections, and their respective shard keys.

sh.status()

Look under the section that lists collections. You'll see entries formatted like:

databases:
  - name: myDatabase
    ...
    collections:
      - name: myDatabase.myCollection
        shardKey: { <shardKeyName>: 1 }

This output shows the database and collection names along with their shard key. The shardKey field displays the key and its direction (1 for ascending, -1 for descending).

Considerations

  • Shard Key Immutability: Once set, the shard key for a collection cannot be changed. If you need a different shard key, you would have to create a new collection, migrate the data, and drop the old collection.
  • Choosing a Shard Key: It's important to choose the shard key carefully, considering factors like write/read distribution, query patterns, and the growth of data.

By checking the shard key, you can better understand how MongoDB distributes data across shards and optimize your queries and indexes accordingly.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.