MongoDB Object.bsonSize()

In MongoDB, you can use the Object.bsonSize() method to return the size of a document in bytes.

Example

Suppose we have a collection called bars with the following document:

{
	"_id" : 1,
	"name" : "Boardwalk Social",
	"location" : {
		"type" : "Point",
		"coordinates" : [
			-16.919297718553366,
			145.77675259719823
		]
	},
	"categories" : [
		"Bar",
		"Restaurant",
		"Hotel"
	],
	"reviews" : [
		{
			"name" : "Steve",
			"date" : "20 December, 2020",
			"rating" : 5,
			"comments" : "Great vibe."
		},
		{
			"name" : "Lisa",
			"date" : "25 October, 2020",
			"rating" : 3,
			"comments" : "They just raised their prices :("
		},
		{
			"name" : "Kim",
			"date" : "21 October, 2020",
			"rating" : 4,
			"comments" : "Nice for Friday happy hour"
		}
	]
}

We can see that the location field contains a document. And the reviews field contains an array of documents.

First, let’s use the Object.bsonSize() method to return the size of the top level document.

Object.bsonsize(db.bars.findOne())

Result:

502

We can see that the whole document is 502 bytes.

Notice that I’m using findOne() and not find(). The reason for this is that find() returns a cursor, rather than the document itself. The findOne() method, on the other hand, returns the actual document and its results should therefore be accurate.

Subdocuments

Let’s use Object.bsonSize() to check the size of the location field.

We can use dot notation to get the value of the location field:

Object.bsonsize(
  db.bars.findOne().location
  )

Result:

61

In this case, the document is 61 bytes.

Just to be sure, here’s what we actually passed to the Object.bsonSize() method:

db.bars.findOne().location

Result:

{
	"type" : "Point",
	"coordinates" : [
		-16.919297718553366,
		145.77675259719823
	]
}

So that’s the document that’s 61 bytes.

Documents in Arrays

We can also retrieve the size of documents that are elements of an array.

Example:

Object.bsonsize(
  db.bars.findOne().reviews[0]
  )

Result:

91

MongoDB arrays are zero-based, so this document is the first review.

We can run the argument by itself to see the whole document:

db.bars.findOne().reviews[0]

Result:

{
	"name" : "Steve",
	"date" : "20 December, 2020",
	"rating" : 5,
	"comments" : "Great vibe."
}

Projections

We can use the Object.bsonSize() method to return the size of the document returned by a projection. To do this, we simply need to provide the projection in our query.

Example:

Object.bsonsize(
    db.bars.findOne(
      {},
      {
        _id: 0,
        location: 1
      }
    )
)

Result:

76

In this case, we get a size of 76.

You might have noticed that in our earlier example, the location field was 61 bytes, but now it’s 76.

What’s going on?

Well, when we use projections, like we are in this example, we’re actually returning an outer document that contains the location field name as well as its value.

Here’s what this projection returns:

db.bars.findOne(
    {},
    {
      _id: 0,
      location: 1
    }
  )

Result:

{
	"location" : {
		"type" : "Point",
		"coordinates" : [
			-16.919297718553366,
			145.77675259719823
		]
	}
}

However, in our earlier example, our query went like this:

db.bars.findOne().location

And returned this:

{
	"type" : "Point",
	"coordinates" : [
		-16.919297718553366,
		145.77675259719823
	]
}

So our projection example returned a larger document, due to the fact that it returned both the field name and the value. And our earlier example returned a smaller document due to the fact that it only returned the value.

Aggregation Pipeline

When using the aggregation pipeline, you can use the $bsonSize operator to get the size of a document.

You can also use the $binarySize operator to get the size of a string or binary value’s content in bytes.