How to check document size before writing?

I have decided on an interaction collection store user interactions:

       userId: "user1",
       blocked: ["user2", "user3"],
       liked: ["user5"]
       userId: "user2",
       blocked: ["user3"],
       liked: ["user1"]

Of course, the document size will be unbounded because each user may block or like any number of other users. So before I update a document, I must check that its size is less than 16mb before writing. How is this done?

Hi @Big_Cat_Public_Safety_Act
if i have understand your question, you can do that in this way:


> Object.bsonsize({userId: "user1",blocked: ["user2", "user3"],liked: ["user5"]})


> Object.bsonsize({userId: "user2",blocked: ["user3"],liked: ["user1"]})

It returns the size in byte of a document. So, in this way you can calculate the size of the object before the updating.
I hope this answer is useful for you!

Best regards

The question is, how to use $bsonSize during a $match stage so that only documents less than certain size will be returned?


I believe this may have been mentioned in some of your other discussion topics, but large documents and unbounded arrays are both schema design anti-patterns that negatively affect performance and efficient use of server resources.

If an insert or update results in a document that is larger than the maximum BSON size, you will get a server exception that your application can handle. However, what would your application do in this case?

I recommend considering a different schema design approach to keep your document sizes more manageable.

Some useful references might be:

You may also find the Socialite - Social Data Reference Architecture helpful. Socialite’s architecture uses multiple services (content, feed, and user graph) that can be scaled independently. There are some Markdown files in the repo (*.md) with some mentions of pros & cons of different approaches.

This seems like a different question, perhaps? If you are reading documents from a remote MongoDB deployment via aggregation the BSON document size should always be less than the limit (or result in an exception if you are trying to create result documents that are too large).