1
0
mirror of https://github.com/jbranchaud/til synced 2026-03-04 06:58:45 +00:00

Add List Size Stats For All Collections as a mongodb til

This commit is contained in:
jbranchaud
2020-06-06 12:54:21 -05:00
parent b1a50708a3
commit ab18922c57
2 changed files with 27 additions and 1 deletions

View File

@@ -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)