5 Ways to Check the Size of a Collection in MongoDB

In this article I present 5 ways that you can use to check the size of a MongoDB collection when using the mongo shell.

The dataSize() Method

Probably the quickest and easiest way to check the size of a MongoDB collection is to use the db.collection.dataSize() method. This method returns the size of the collection in bytes.

To call this method, simply replace collection with the name of the collection that you’d like to check.

Example:

db.posts.dataSize()

Result:

3012

This example checks the size of a collection called posts. It’s only a small collection with a handful of small documents, and so the size is only 3012 bytes.

The totalSize() Method

The db.collection.totalSize() method returns the total size in bytes of the data in the collection plus the size of every index on the collection.

Therefore, use this method if you want to know the total size that includes all indexes.

Example:

db.posts.totalSize()

Result:

114688

This is the same collection that we ran the previous example against. The posts collection has three indexes (including the one on the _id field), and we can see that the total size is significantly higher than the collection size without the indexes.

The stats() Method

If you need more than just the collection size, you could try the db.collection.stats() method.

This method returns statistics about the collection (including its size).

Example:

db.posts.stats()

Result (partial):

{
	"ns" : "krankykranes.posts",
	"size" : 3012,
	"count" : 27,
	"avgObjSize" : 111,
	"storageSize" : 36864,
	"freeStorageSize" : 16384,
	"capped" : false,
	"wiredTiger" : {
		"metadata" : {
			"formatVersion" : 1
		},
...

This method returns a lot of data, so I’m only returning the first small part here. The collection size is listed near the top of the results, in the size field.

By default, this method returns size data in bytes. You can change it so that it reports in kilobytes if you prefer. To do this, use the scale parameter with a value of 1024.

Example:

db.posts.stats( { scale: 1024 } )

Result (partial):

{
	"ns" : "krankykranes.posts",
	"size" : 2,
	"count" : 27,
	"avgObjSize" : 111,
	"storageSize" : 36,
	"freeStorageSize" : 16,
	"capped" : false,
	"wiredTiger" : {
		"metadata" : {
			"formatVersion" : 1
		},
...

The db.collection.stats() method provides a wrapper around the collStats command (below).

The collStats Command

As mentioned, the db.collection.stats() method is a wrapper for the collStats diagnostic command. This command returns statistics about the collection (including its size).

Example:

db.runCommand( { collStats : "posts", scale: 1 } )

Result (partial):

{
	"ns" : "krankykranes.posts",
	"size" : 3012,
	"count" : 27,
	"avgObjSize" : 111,
	"storageSize" : 36864,
	"freeStorageSize" : 16384,
	"capped" : false,
	"wiredTiger" : {
		"metadata" : {
			"formatVersion" : 1
		},
...

As with the stats() method, this command returns a lot of data, so I’ve just grabbed some stuff off the top.

Note that I provided a scale of 1 in this example to return size data in bytes. We can change that to 1024 to return it in kilobytes.

Example:

db.runCommand( { collStats : "posts", scale: 1024 } )

Result (partial):

{
	"ns" : "krankykranes.posts",
	"size" : 2,
	"count" : 27,
	"avgObjSize" : 111,
	"storageSize" : 36,
	"freeStorageSize" : 16,
	"capped" : false,
	"wiredTiger" : {
		"metadata" : {
			"formatVersion" : 1
		},
...

The $bsonSize Operator

The $bsonSize aggregation pipeline operator was introduced in MongoDB 4.4 for the purpose of returning the size of a BSON document.

You can use $bsonSize to return the total size of all documents in the collection by combining it with the $group and $sum operators.

Example:

db.posts.aggregate([
  {
    $group: {
      "_id": null,
      "rootSize": { $sum: { $bsonSize: "$$ROOT" } }
    }
  }
])

Result:

{ "_id" : null, "rootSize" : 3012 }