0

I am using MongoDB with Next.js. What I am trying to do is, I try to add as many sub-documents as possible to so that I can reduce the lookup time when doing a find operation. However, the maximum document is 16Mb, I will create a new document and add the sub-document to a new document if the new size will exceed 16MB.

Is there a way to check the size of the document before the insertion? For example, the current size of the document is 15mb, and I have a 2mb sub-document. I see that 15+2==17 which is greater than 16. So, we will create a new document for this new sub-document.

Rongeegee
  • 866
  • 3
  • 10
  • 30
  • Have a look at [$bsonSize](https://www.mongodb.com/docs/v6.0/reference/operator/aggregation/bsonSize/) or https://stackoverflow.com/a/70697559/3027266 and https://stackoverflow.com/questions/34767213/how-to-get-size-of-mongodb-single-document-in-bytes-using-nodejs/34768008#34768008 – Wernfried Domscheit Sep 23 '22 at 21:13

0 Answers0