Calculate bsonSize of the object in NodeJs

I can see the document size in BSON using the below command.

db.myCollection.aggregate([
  {
    $project: {
      size_bytes: { $bsonSize: "$$ROOT" },
      size_KB: {
        $divide: [{ $bsonSize: "$$ROOT" }, 1000],
      },
      size_MB: {
        $divide: [
          { $bsonSize: "$$ROOT" },
          1000000,
        ],
      },
    },
  },
])

But when I insert an object to the document from nodeJs I need to calculate the size of the document, just to make sure it does not exceed the 16MB document limit. If it does, then I wanted to create another document and keep inserting multiple objects into the newly created document. So every time I do not want to run aggregation on the database instance before inserting a new object into the document.

I tried using the BSON library which can calculate the BSON size of the JSON object before inserting it into the document. But it seems it does not calculate BSON size correctly.

Can someone please suggest to me the correct way to calculate the BSON size of the object which I want to insert into the document?

The calculateObjectSize method from the BSON library should be able to do this:

const bson = require("bson");
new bson.calculateObjectSize(doc)

Can you share how you were doing this or why you believe the size reported is incorrect?

I tried the same way. But the size of all objects present in one document does not match with size which I calculate from the mongo command line interface using $bsonSize.

Thank you for your help. But now I am using bson from mongo. It works with the same approach. But the library of bson which I installed using ‘npm install bson’ does not provide me correct object size.

Hi @Prasanna_Sasne, we recently opened NODE-5153 as BigInts aren’t properly supported by calculateObjectSize.

In the event there are additional issues you may have surfaced as a result of your tests, would you be able to share a complete failing test that showcases:

  1. a call to aggregate that includes a $bsonSize (producing the “correct” results)
  2. a call to calculateObjectSize that produces an incorrect result
  3. sample document(s) used to reproduce these scenarios

This should help us properly identify where the discrepancy in these calculations may be occurring.

Sure. I will add all details. Thank you