-
-
Save arthuralvim/ddcf14783a00189ad48f69acd38372eb to your computer and use it in GitHub Desktop.
List collections, size and count of documents on MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
MONGO_URI = '' | |
DATABASE_NAME = '' | |
client = MongoClient(MONGO_URI) | |
db = client[DATABASE_NAME] | |
collections = db.collection_names() | |
def readable_size(file_size): | |
index_unit = 0 | |
units = ['byte', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | |
while (file_size > 1024): | |
file_size = file_size / 1024; | |
index_unit += 1 | |
return "{0:.2f} {unit}".format(round(file_size, 2), unit=units[index_unit]) | |
stats = {} | |
for collection in collections: | |
col_stats = db.command('collstats', collection) | |
stats[collection] = { | |
'count': col_stats['count'], | |
'size': readable_size(col_stats['size']) | |
} | |
print collection + ' Collection size: {size} / Documets count: {count}'.format( | |
size=stats[collection]['size'], | |
count=stats[collection]['count']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment