Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view sharding keys in mongodb shell?

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?

like image 233
Morgan Cheng Avatar asked Apr 02 '12 14:04

Morgan Cheng


3 Answers

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.

like image 190
Brendan W. McAdams Avatar answered Oct 22 '22 17:10

Brendan W. McAdams


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.

like image 14
Marc Qualie Avatar answered Oct 22 '22 18:10

Marc Qualie


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.

like image 2
Gandalf the White Avatar answered Oct 22 '22 17:10

Gandalf the White