Skip to content

Instantly share code, notes, and snippets.

@fancyremarker
Last active December 12, 2017 02:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fancyremarker/2493262 to your computer and use it in GitHub Desktop.
Save fancyremarker/2493262 to your computer and use it in GitHub Desktop.
List MongoDB collections in order of size on disk (for collection and indexes)
DB=your_mongo_database
for coll in `mongo ${DB} --quiet --eval "db.getCollectionNames()" | tr ',' ' '`; do
echo -n `mongo ${DB} --quiet --eval "db['${coll}'].stats(1024)['size'] + db['${coll}'].stats(1024)['totalIndexSize']"`
echo ": ${coll}"
done | sort -n -r
@dblock
Copy link

dblock commented Jul 9, 2013

In Mongoid:

Mongoid.default_session.collections.map { |c| s = Mongoid.default_session.command(collstats: c.name); [ c.name, s["size"] + s["totalIndexSize"]] }.sort_by { |n, s| -s }.take(10).each { |n, s| puts "#{n}: #{s}" }; nil

@graemeboy
Copy link

Update for Mongoid:

Mongoid.default_client.collections.map { |c| s = Mongoid.default_client.command(collstats: c.name).documents[0]; [s['ns'], s['size'] + s['totalIndexSize']] }.sort_by { |n, s| -s }.take(10).each { |n, s| puts "#{n}: #{s}" }; nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment