How To
How to get the Bson size of records in MongoDb?
3 min
using mongo queries querying via the db this will give the applicationid db getcollection("applications") find({'name'\ application name"},{" id" 1}) to get the recordid db records find({'name' "tiia 211993"},{" id" 1}) then you can run the following object bsonsize(db getcollection("records") findone({" id" "{{recordid }}"},{'{{values}}' 1}) 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 object bsonsize(db getcollection("records") findone({" id" "{{recordid }}"},{'{{valuesdocument}}' 1}) using python script import bson from pymongo import mongoclient import datetime import ssl from swimlane import swimlane import pprint from bson raw bson import rawbsondocument """ connecting to swimlane """ sw = swimlane(host="{host url}", access token=sw context inputs\['swimlane pat'], verify ssl=false) """ connecting to mongo document class=rawbsondocument is added to fetch the record as bson """ client = mongoclient(host=sw context inputs\['mongo hostname'],# port=sw context inputs\['mongo port'], username=sw context inputs\['mongo username'], password=sw context inputs\['mongo password'], authsource=sw context inputs\['mongo auth source'], authmechanism='scram sha 1', ssl=true, ssl cert reqs=ssl cert none, document class=rawbsondocument) """ assigning the database """ db = client swimlane list database names = client list database names() """ all the records are in collection called records """ collection = db\['records'] """ fetch the record using id the condition can be altered w\ r t the available params please note that the whole record size include some infrastructural data, to get the data occupied by the field values and field documets specifically up the value of fields dict below to 1 you can also include any more data points required like applicationid,referenserecords etc which vary according to the application built do a print(collection find({' id' 'akzuc1wbtsvck1wb4'}) to get the whole record structure 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)) } """ record id = "xxxxxxxxxxx" #eg\ akzuc1wbtsvck1wb4 fields = {'values' 0,'valuesdocument' 0} document = collection find({' id'\ record id},fields) """ document will be a cursor with the list of results """ for doc in document print(len(doc raw)) use cases to find if the record is exceeding the size limit of 16mb """ 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 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")