From b1a50708a3bc80f2149aa98b2b80f084ddb75eba Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 6 Jun 2020 12:41:20 -0500 Subject: [PATCH] Add Get Size Stats For A Collection as a mongodb til --- README.md | 3 ++- mongodb/get-size-stats-for-a-collection.md | 30 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 mongodb/get-size-stats-for-a-collection.md diff --git a/README.md b/README.md index a5f088f..925dcab 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). -_922 TILs and counting..._ +_923 TILs and counting..._ --- @@ -392,6 +392,7 @@ _922 TILs and counting..._ ### MongoDB - [Determine The Database Version](mongodb/determine-the-database-version.md) +- [Get Size Stats For A Collection](mongodb/get-size-stats-for-a-collection.md) ### MySQL diff --git a/mongodb/get-size-stats-for-a-collection.md b/mongodb/get-size-stats-for-a-collection.md new file mode 100644 index 0000000..e8e2583 --- /dev/null +++ b/mongodb/get-size-stats-for-a-collection.md @@ -0,0 +1,30 @@ +# Get Size Stats For A Collection + +For any collection in your MongoDB instance. + +```javascript +> db.getCollectionNames() +["books", "authors", "genres"] +``` + +You can list a collection of stats, which include the amount of disk space that +collection is utilizing. + +```javascript +> db.books.stats().size +11057056 +``` + +By default this size is in bytes, which isn't all that human-readable of a +value. + +By passing in a `scale` value to `stats()`, you can get a value that is a bit +more understandable. A scale of `1024` would give you kilobytes, so a scale of +`1024 * 1024` would give you megabytes. + +```javascript +> db.books.stats({ scale: 1024 * 1024 }).size +10 +``` + +[source](https://docs.mongodb.com/manual/reference/method/db.collection.stats/)