15

This might be a dummy question, but I cannot find any clue in all online doc.

For a already-built-up mongodb cluster, How can I find which sharding key(s) is used for given collecton?

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230

3 Answers3

18

As outlined in the Sharding Administration Docs, you can use db.printShardingStatus() to see this information.

For sharded collections it will print the key pattern.

Brendan W. McAdams
  • 9,379
  • 3
  • 41
  • 31
14

You can login to any "mongos" instance (or config server instance) on the cluster and query against the collections collection.

use config
db.collections.find()

You can also do this from any of the drivers since it's just like running a normal query. This collection stores information about all of the sharded collections in your cluster and which keys they are sharded on.

Marc Qualie
  • 763
  • 7
  • 13
3

Just use the normal status command.

sh.status()

Output copied from my shell for reference

  databases:
        {  "_id" : "Test1",  "primary" : "atlas-<hidden>",  "partitioned" : true,  "version" : {  "uuid" : UUID("<hidden>"),  "lastMod" : 1 } }
                Test1.TestCollection
                        shard key: { "location" : 1, "userid" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                atlas-<hidden>  172

Under databases section you will see the complete detail.

Gandalf the White
  • 2,415
  • 2
  • 18
  • 39