diff --git a/README.md b/README.md index 925dcab..66e59b6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_923 TILs and counting..._ +_924 TILs and counting..._ --- @@ -393,6 +393,7 @@ _923 TILs and counting..._ - [Determine The Database Version](mongodb/determine-the-database-version.md) - [Get Size Stats For A Collection](mongodb/get-size-stats-for-a-collection.md) +- [List Size Stats For All Collections](mongodb/list-size-stats-for-all-collections.md) ### MySQL diff --git a/mongodb/list-size-stats-for-all-collections.md b/mongodb/list-size-stats-for-all-collections.md new file mode 100644 index 0000000..561dc67 --- /dev/null +++ b/mongodb/list-size-stats-for-all-collections.md @@ -0,0 +1,25 @@ +# List Size Stats For All Collections + +In [Get Size Stats for a Collection](get-size-stats-for-a-collection.md), we +saw how to use `db.collection.stats()` and its `scale` parameter to get a +useful size metric for a given collection. + +We can combine some of this concepts with some scripting to list human-readable +size metrics for all of our database's collections: + +```javascript +> db.getCollectionNames().forEach(function (collectionName) { + sizeInMb = db[collectionName].stats({ scale: 1024 * 1024 }).size; + print(collectionName + ": " + sizeInMb + "Mb"); + }) +books: 10Mb +authors: 2Mb +genres: 1Mb +``` + +This snippet gets all the collections for the current database and iterates +over them. For each collection name, it looks up the `size` stat for that +collection scaled to megabytes and then prints it all out with some contextual +information. + +[source](https://docs.mongodb.com/manual/faq/storage/#data-storage-diagnostics)