4 / 4
Mar 19

Hi Team,

I’m new to mongodb and we are planning to use sharding feature in our company, so it became a priority for me to monitor it for shardDistribution, so i used pymongo example: db.collection_name.getShardDistribution() but it is not working, and I’m not sure what I’m missing
can someone help me with this?

Thanks
Vaseem

Hi @Vaseem_Akram_mohammad and welcome to the MongoDB community forum!!

The db.collection.getShardDistribution is a helper function which works only in the mongosh as of today.
The driver version of the command in pymongo are still not present in the latest release.

While there is no direct replacement of the command in Python, you may be able to get some part of the information by switching to the config database and perform some queries on the collections inside it. See Collections to Support Sharded Cluster Operations for more details on this.

However, please note that the contents of the config database are internal to MongoDB and may be subjected to change in future depending on the product requirement.

Let us know if you have any further queries.

Best Regards
Aasawari

20 days later
2 years later

Hi Vaseem

From pymongo you can call the aggregation stage $shardedDataDistribution in order to retrieve the shard distribution of sharded collections.

Here an example

import pymongo client = pymongo.MongoClient() pipeline = [{"$shardedDataDistribution":{}}] c=client["admin"].aggregate(pipeline) print(c.next()) {'ns': 'myDB.mycoll 'shards': [{'shardName': 'shard01', 'numOrphanedDocs': 0, 'numOwnedDocuments': 0, 'ownedSizeBytes': 0, 'orphanedSizeBytes': 0}]}