MongoDB Collection Size

Introduction

In this article we’ll look at finding out the size of your MongoDB collection. Whether you’re looking for the number of documents in the collection or the amount of storage the collection is taking up we will show you how to get both in a simple easy way. We’ll first show you how to do it in the MongoDB shell because the syntax should be similar for whatever driver you’re using whether it be for NodeJS, PHP, Python, or another driver.

Prerequisites

  • You should have MongoDB installed on your system.
  • You can follow along using the MongoDB Shell or with whatever driver you’re using, the syntax across the drivers is very similar.

Use the db.collection.stats() function

We can get all the information we need about the size of our collection using the stats() function on a collection.

We think most developers learn by example so let’s jump straight into an example of using it in the MongoDB Shell and then we’ll examine the details afterward. Locally we have a sample database grocerydb for a small grocery store. Inside grocerydb is a products collection that contains the following documents:

> use grocerydb
switched to db grocerydb
> db.products.find()
{
        "_id" : ObjectId("5d12c9974b74efae4662b8df"),
        "name" : "Soda",
        "quantity" : 10,
        "price" : 5.5
}
{
        "_id" : ObjectId("5d12c9a34b74efae4662b8e0"),
        "name" : "Bread",
        "quantity" : 9,
        "price" : 2.5
}
{
        "_id" : ObjectId("5d12c9b24b74efae4662b8e1"),
        "name" : "Milk",
        "quantity" : 4,
        "price" : 3.5
}

Now let’s run the stats command on the collection:

> db.products.stats()

This returns a wealth of data on the collection so we’ve filtered out some of the contents for clarity:

{
        "ns" : "grocerydb.products",
        "size" : 211,
        "count" : 3,
        "avgObjSize" : 70,
        "storageSize" : 36864,
        "capped" : false,
        "wiredTiger" : {
                "metadata" : {
                        "formatVersion" : 1
                },
                },
                "block-manager" : {
                        "allocations requiring file extension" : 0,
                },
                "btree" : {
                        "btree checkpoint generation" : 22,
                },
                "cache" : {
                        "bytes currently in the cache" : 624,
                },
                "cache_walk" : {
                        "Average difference between current eviction generation when the page was last considered" : 0,
                },
                "compression" : {
                        "compressed pages read" : 0,
                },
                "cursor" : {
                        "bulk-loaded cursor-insert calls" : 0,
                },
                "reconciliation" : {
                        "dictionary matches" : 0,
                },
                "session" : {
                        "cached cursor count" : 1,
                },
                "transaction" : {
                        "update conflicts" : 0
                }
        },
        "nindexes" : 1,
        "totalIndexSize" : 24576,
        "indexSizes" : {
                "_id_" : 24576
        },
        "ok" : 1
}

Let’s filter it down further to the properties that we think you’re looking for:

{
        "ns" : "grocerydb.products",
        "size" : 211,
        "count" : 3,
        "avgObjSize" : 70,
        "storageSize" : 36864,
        "capped" : false,
        "nindexes" : 1,
        "totalIndexSize" : 24576,
        "indexSizes" : {
                "_id_" : 24576
        },
        "ok" : 1
}

ns – Specifies the database and collection in the format of . and is a good sanity check to make sure you’re looking at data for the correct collection. size – The size in memory of all the documents in this collection. This is the uncompressed size. It also does not include the size of any indexes. This value is in bytes unless you decide to change it by using the scale parameter. count – The number of documents in this collection.

We think these are the values you’ll mostly be concerned with but below are a few others that are used frequently:

nindexes – The number of indexes on the collection. totalIndexSize – The size in memory of the indexes on the collection. Again this is in bytes unless you change it explicitly using the scale parameter.

Want to try it yourself?

If you want to run the same example yourself here’s all the commands you’ll need to run in the MongoDB Shell:

Note: If you’re not sure how to start the MongoDB Shell please do the following which will get you into the MongoDB Shell as long as you have MongoDB installed. First go to a shell and run the mongod command which will run MongoDB in the background. Then in another terminal tab run the mongo command which will take you into the MongoDB Shell and prompt you with >.

> use grocerydb
> db.createCollection("products")
> db.collection.insert(
[{
        "name" : "Soda",
        "quantity" : 10,
        "price" : 5.5
}
{
        "name" : "Bread",
        "quantity" : 9,
        "price" : 2.5
}
{
        "name" : "Milk",
        "quantity" : 4,
        "price" : 3.5
}]
)
> db.products.stats()

Conclusion

In this article we showed you to determine the number of documents in a collection and get the size in bytes of that collection using the stats() function. You can perform this same operation using any of the drivers available for almost every language which can be downloaded from MongoDBs official site.

If you want to put your MongoDB data in some responsible hands please don’t hesitate to reach out to us at Object Rocket. We manage production data all day every day and are always happy to help.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed Redis,
MongoDB & Elasticsearch

Get Started

OR

Try CockroachDB
in Beta

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.