How to get collection size in MongoDB

In this MongoDB tutorial, we will understand how to get collection size in MongoDB. Furthermore, We will cover the following list of topics:

  • MongoDB get collection size
  • MongoDB get collection size example
  • MongoDB get size of a document from collection
  • MongoDB get collection size in GB
  • MongoDB get collection size in Ubuntu
  • MongoDB get collection size using python

MongoDB get collection size

In MongoDB, the totalSize() method is used to find the total size of a collection, including the size of all documents and all indexes on a collection in MongoDB.

Syntax:

db.collection_name.totalSize()

Note:

The value returned is the sum of db.collection.storageSize() and db.collection.totalIndexSize() in bytes.

Read $in MongoDB Example

MongoDB get collection size example

In this topic, you will learn to get out the MongoDB collection size with the help of an example.

Example:

In this example, we are using an existing collection post of the test database.

The subsequent documents are inside the example collection:

db.example.find()
{ "_id" : 1, "name" : "John", "address" : "USA" }
{ "_id" : 2, "name" : "Peter", "address" : "United States" }
{ "_id" : 3, "name" : "Amy", "address" : "Canada" }
{ "_id" : 4, "name" : "Hannah", "address" : "United Kingdom" }
{ "_id" : 5, "name" : "Michael", "address" : "Australia" }
{ "_id" : 6, "name" : "John", "address" : "New Zealand" }

Now, we apply the below query to return the total size in bytes of the data in the collection plus the size of every index on the MongoDB collection.

db.example.totalSize()

Output:

MongoDB get collection size
MongoDB get collection size

In the output, you can see, we have successfully got the total collection size.

Note that, there is one more way to get the collection size the dataSize() command but this will only return the size in bytes for the specified data.

db.collection_name.dataSize()
MongoDB get collection size example
MongoDB get collection size

The amount of time required to return dataSize depends on the amount of data in the collection.

Read Current date in MongoDB

MongoDB get size of a document from collection

In this topic, you will learn to get the size (in bytes) of the document. MongoDB provides the bsonSize(), operator to return the size of the cursor rather than the document. And, This will return the correct size of a particular document.

Example:

In this example for reference, we are using the existing collection relation of the myDB database.

db.relation.find().pretty()
{
        "_id" : ObjectId("6165078e016e7b648abb7fa6"),
        "subject" : "Joe",
        "content" : "best friend",
        "likes" : 60,
        "year" : 2015,
        "language" : "english"
}
{
        "_id" : ObjectId("6165078e016e7b648abb7fa7"),
        "subject" : "Dogs",
        "content" : "Cats",
        "likes" : 30,
        "year" : 2015,
        "language" : "english"
}
{
        "_id" : ObjectId("6165078e016e7b648abb7fa8"),
        "subject" : "Cats",
        "content" : "Rats",
        "likes" : 55,
        "year" : 2014,
        "language" : "english"
}
{
        "_id" : ObjectId("6165078e016e7b648abb7fa9"),
        "subject" : "Rats",
        "content" : "Joe",
        "likes" : 75,
        "year" : 2014,
        "language" : "english"
}

We will apply the below query to get the size of a document from the info collection.

Object.bsonsize(db.relation.findOne({ "_id" : ObjectId("6165078e016e7b648abb7fa9") }))

Here, we have used the findOne() method to find the size of a single document. We have also defined an object id in the findOne() method, if you don’t specify then by default it will take the first document.

MongoDB get size of a document in collection
MongoDB get the size of a document from the collection

Note:

In your bsonSize() example, if you are measuring the size by using the find() method that it will return a cursor rather than a document.

So to find the size of a single document in MongoDB, you should instead be using a findOne() method.

Read Display MongoDB data in HTML

MongoDB get collection size in GB

In this topic, you will learn to get the collection size in GB in MongoDB. For that, MongoDB provides the stats() function to determine document size.

Example:

Here, we are using the relation collection for reference you can check the previous topic.

MongoDB get collection size in GB
MongoDB get collection size in GB

We will apply the below query to get collection size in GB:

db.relation.stats(1024*1024*1024)

Here, we have used “1024*1024*1024” in the stats() function to get collection size in GB.

Get the collection size in GB in MongoDB
Get the collection size in GB in MongoDB

Note:

By default, the stats() function returns in the byte format. You need to pass the argument to see it in a different format:

#Results in Byte:

db.collection_name.stats()

#Results in KB:

db.collection_name.stats(1024)

#Results in MB:

db.collection_name.stats(1024*1024)

#Results in GB:

db.collection_name.stats(1024*1024*1024)

Read Distinct query in MongoDB

MongoDB get collection size in Ubuntu

In this topic, you will learn to get out collection size in the Ubuntu operating system. Here, we also use the totalSize() method to find the total size of a collection, including the size of all documents and all indexes on a collection.

Example:

In this example, we are using the existing collection animal, For reference check below:

db.animal.find().pretty()
{
	"_id" : ObjectId("61934ee50ab85e2390d6177e"),
	"subject" : "Joe",
	"content" : "best friend",
	"likes" : 60,
	"year" : 2015,
	"language" : "english"
}
{
	"_id" : ObjectId("61934ee50ab85e2390d6177f"),
	"subject" : "Dogs",
	"content" : "Cats",
	"likes" : 30,
	"year" : 2015,
	"language" : "english"
}
{
	"_id" : ObjectId("61934ee50ab85e2390d61780"),
	"subject" : "Cats",
	"content" : "Rats",
	"likes" : 55,
	"year" : 2014,
	"language" : "english"
}
{
	"_id" : ObjectId("61934ee50ab85e2390d61781"),
	"subject" : "Rats",
	"content" : "Joe",
	"likes" : 75,
	"year" : 2014,
	"language" : "english"
}

We will apply the below query to get the collection size:

db.animal.totalSize()

It will return the total size in bytes of the data in the collection plus the size of every index on the collection.

MongoDB get collection size in Ubuntu
MongoDB get collection size in Ubuntu

We have successfully got the total collection size using ubuntu in MongoDB.

Read MongoDB find by ID

MongoDB get collection size using python

In this topic, you will learn to get the collection size using python.

For that, we will the pymongo library to use the MongoDB functionality and MongoDB provides the collstats and dbstats commands that will give you useful information on collection and database size.

Let us understand this with the help of an example and find the size of a collection.

Example:

In this example, we are using the existing database and collection, Company and artists respectively.

import pymongo

# creating a MongoClient object and connect with the host
myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017")

# accessing the database
mydb = myclient["Company"]

# print collection statistics
size = mydb.command("collstats", "artists")
print(size)

In the code, we have followed the below steps:

  • Import the pymongo library to use MongoDB in python.
  • After that, create a MongoClient object and connect with the host
  • Access the database Company
  • Next, we have used the command() function and defined parameter collstats and artists.
  • The collstats is used to print the collection statistics. After that, you can define any collection name for that you want to find collection statistics, here we have defined artists.
  • After that print the result.
MongoDB get collection size using python
MongoDB get collection size using python

We have successfully got the collection statistics and here you can also check the size of the collection in MongoDB.

You may also like the following MongoDB tutorials:

So, In this tutorial, we have understood to find out the collection size in MongoDB. Additionally, We have covered the following list of topics:

  • MongoDB get collection size
  • MongoDB get collection size example
  • MongoDB get size of a document from collection
  • MongoDB get collection size in GB
  • MongoDB get collection size in Ubuntu
  • MongoDB get collection size using python