0

I'm trying to figure out, how to calculate size before storing some data to MongoDB. Since I wrote a script that parses data, combining them in a single document and trying to do instance.save().

So far I am getting an error such as:

BSONObj size: 17102819 (0x104F7E3) is invalid. Size must be between 0 and 16793600(16MB)

I don't want to use gridFS, because it is architecture-dependent and the project is big enough to use it, so far I decided to skip such documents that have a greater size than required.

I found the article about: How to calculate existing document BSONSize

Link to the article: https://betterprogramming.pub/how-to-calculate-mongodb-document-size-in-node-js-f463b8457f27

But, I need to figure out, how to calculate it before storing it in the database

Daniil Sinelnik
  • 244
  • 1
  • 5
  • 13

1 Answers1

0

Try this:

Object.bsonsize({field: "value"})
-> 22

Look like this method is available only in legacy shell mongo but not in new mongosh. For mongosh, see https://stackoverflow.com/a/34768008/3027266

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110