Sharding status with shard keys (sh.status()) using mongo ruby driver

Hi. I want to get the results of sh.status() but using the ruby mongo driver. Specifically I need to see the shard keys for each collection, not just the hosts for each shard.

I’m looking for something similar to the output of running sh.status() on a mongos:

                        shard key: { "_id" : "hashed" }
                        unique: false
                        balancing: true
                                chunk-1	121520
                                chunk-2	121520

How do I do this using Ruby mongo?

I’ve already tried

client.command(serverStatus: 1)
client.command(getShardMap: 1)
client.command(listShards: 1)
client.command(listCollections: 1)

The first 3 give only the hosts of the shards and the 4th only collection data, but not the shard keys of the collections. I’ve also tried getting the contents of the config.databases collection, that doesn’t include the shard keys either.

@Neil_Ongkingco, the information you’re looking for is in the Config Database, which you can query directly using the Ruby driver.

For example, to list the sharded collections in your cluster you could try something similar to the following:

require 'mongo'
client ="mongodb://localhost:27017/test")
configdb = client.use(:config)
configdb[:collections].find.each do |c|
  puts c["_id"]
  puts "\tshard key: #{c['key']}"
  puts "\tunique: #{c['unique']}"
  puts "\tbalancing: #{!c['noBalance']}"

For the test cluster I set up the output was:
	shard key: {"_id"=>"hashed"}
	unique: false
	balancing: true
	shard key: {"a"=>1.0, "b"=>1.0}
	unique: false
	balancing: true
	shard key: {"_id"=>1}
	unique: false
	balancing: true

All we’re doing here is querying the config.collections collection and formatting the results. If you wanted to show chunk details, as you iterate over each sharded collection above you could query the config.chunks collection to collect the necessary information.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.