Search code examples
mongodbshardingmongo-shell

How to get the shard key of a given collection with MongoDB shell?


For investigation purpose I need to get the shardkey of a given MongoDB collection.

db.getSiblingDB("mydb").mycollection.getIndexes() gives all indexes. db.getSiblingDB("mydb").mycollection.getShardDistribution() gives the distribution among shards.

What is the command to use in order to get the shard key?


Solution

  • You can use db.printShardingStatus(). See documentation here