---
title: How to get the Bson size of records in MongoDb?
slug: user-guide-10x/how-to-get-the-bson-size-of-records-in-mongodb
docTags: 
createdAt: 2025-12-09T14:32:34.881Z
---

**Using Mongo Queries:**

Querying via the DB :

- This will give the ApplicationID:

:::BlockQuote{indent="2"}
db.getCollection("Applications").find(\{'Name'\:Application Name"},\{"\_id":1})
:::

- To get the RecordId:&#x20;

:::BlockQuote{indent="2"}
db.Records.find(\{'Name':"TIIA-211993"},\{"\_id":1})
:::

- Then you can run the following:

:::BlockQuote{listStyleType="disc" listStart="4" indent="1"}
Object.bsonsize(db.getCollection("Records").findOne(\{"\_id": "\{\{RecordId }}"},\{'\{\{Values}}':1})&#x20;
:::

Make sure to use 'findOne' instead of 'find' to get the records for fetching the record size as the 'find' statement gives a cursor as output.

'Values' to retrieves only the memory occupied by fields
'ValuesDocument' to retrieve the memory occupied by documents

:::BlockQuote
Object.bsonsize(db.getCollection("Records").findOne(\{"\_id": "\{\{RecordId }}"},\{'\{\{ValuesDocument}}':1})&#x20;
:::

**Using Python Script:**

:::BlockQuote
import bson
from pymongo import MongoClient
import datetime
import ssl
from swimlane import Swimlane
import pprint
from bson.raw\_bson import RawBSONDocument


"""
&#x20;  &#x20;
&#x20;    Connecting to Swimlane
&#x20;      &#x20;
"""
sw = Swimlane(host="\{host url}", access\_token=sw\_context.inputs\['swimlane\_pat'], verify\_ssl=False)


"""
&#x20;  &#x20;
&#x20;    Connecting to mongo
&#x20; document\_class=RawBSONDocument is added to fetch the record as bson&#x20;
&#x20;      &#x20;
"""

client = MongoClient(host=sw\_context.inputs\['mongo\_hostname'],#
&#x20;                    port=sw\_context.inputs\['mongo\_port'],&#x20;
&#x20;                    username=sw\_context.inputs\['mongo\_username'],&#x20;
&#x20;                    password=sw\_context.inputs\['mongo\_password'],&#x20;
&#x20;                    authSource=sw\_context.inputs\['mongo\_auth\_source'],&#x20;
&#x20;                    authMechanism='SCRAM-SHA-1',&#x20;
&#x20;                    ssl=True,&#x20;
&#x20;                    ssl\_cert\_reqs=ssl.CERT\_NONE,
&#x20;                    document\_class=RawBSONDocument)

"""

&#x20;   Assigning the Database
&#x20;      &#x20;
&#x20;&#x20;
"""

db = client.Swimlane&#x20;



list\_database\_names = client.list\_database\_names()

"""

&#x20; All the records are in collection called Records
&#x20;  &#x20;
"""

collection = db\['Records']


"""

&#x20; Fetch the record using \_id
&#x20;   the condition can be altered w\.r.t the available params
&#x20;  &#x20;
&#x20;   Please note that the whole record size include some infrastructural data,
&#x20;   To get the data occupied by the field values and field documets specifically&#x20;
&#x20;   Up the value of fields dict below to 1
&#x20;   You can also include any more data points required like ApplicationId,ReferenseRecords etc which vary according to the application built
&#x20;  &#x20;
&#x20;   Do a print(collection.find(\{'\_id':'aKzuC1WBtsVcK1Wb4'}) to get the whole record structure&#x20;
&#x20;  &#x20;
&#x20;   PS: if you have set RawBSONDocument as document\_class the output has to be decoded eg: bson.decode(doc.raw)} \{ # doc----> for doc in document: print(len(doc.raw)) }

&#x20;  &#x20;
&#x20;  &#x20;
"""
record\_id = "xxxxxxxxxxx" #Eg\:aKzuC1WBtsVcK1Wb4
fields = \{'Values':0,'ValuesDocument':0}
document = collection.find(\{'\_id'\:record\_id},fields)

"""&#x20;

&#x20; Document will be a cursor with the list of results
&#x20;  &#x20;
"""

for doc in document:
&#x20; print(len(doc.raw))


:::

**Use Cases:**

- To find if the record is exceeding the size limit of 16mb:

:::BlockQuote{indent="1"}
""" Document fields are excluded here as the limit is confined to 16mb excluding the attachments"""

fields = \{'Values':1,'ValuesDocument':0}
document = collection.find(\{'\_id':'\{recordid}'},fields)

for doc in document:
&#x20; doc\_len = len(doc.raw)

\#16777216 = 16mb

memory\_used =  (100-((16777216 -doc\_len)/16777216)\*100)
print(f"The record occupies \{memory\_used}% of the available limit")
:::

