mjsr
mjsr

Reputation: 7590

is BSON representation minified?

I'm working with mongo, one of the documents I'm saving exceeded the maximum 16mb. I wonder if minifying the structure could help in that aspect. So that's why I have the question on the title. If the bson representation already minify the document, then another attemp by my side it doesn't going to help in anything.

Upvotes: 4

Views: 715

Answers (4)

ubik
ubik

Reputation: 605

Document keys eat up a lot of space in BSON since they are stored verbatim, so if you can encode them you'll save a lot of bytes. It can be acceptable if your code will be the only reader of those data, so that no human will have to remember that

const MY_INSANELY_LONG_OBJECT_PROPERTY_NAME = "a";

var thePropertyValue = myObject[MY_INSANELY_LONG_OBJECT_PROPERTY_NAME];

Upvotes: 2

kevinadi
kevinadi

Reputation: 13765

MongoDB stores data in a BSON representation, where it preserves field names and contents. The total object size depends on the length of the fields as well as the content of the fields. If you're willing to abbreviate your field names, then you could make your document smaller.

You can quickly check this by inserting a document with a long and a short field name:

> db.test.insert({abcde:1})
> db.test.stats()
{
  "ns": "test.test",
  "count": 1,
  "size": 37,
  "avgObjSize": 37,
  ...

If you can abbreviate the field name, you can achieve some reduction in object size:

> db.test2.insert({a:1})
> db.test2.stats()
{
  "ns": "test.test2",
  "count": 1,
  "size": 33,
  "avgObjSize": 33,
  ...

From the two examples above, shortening the field name from abcde to just a resulted in reduction in object size: 37 bytes vs 33 bytes, which is a saving of 4 bytes using a shorter field name.

Upvotes: 2

Arka Ghosh
Arka Ghosh

Reputation: 855

If you document size become more than 16MB even after minifying it, you can use GridFS to break that documnet up.

As per MongoDB documentation,

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB.

Hope this helps..

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43235

BSON is already a highly compact format,moreover WiredTiger engine compresses data at page level, not document level.

If you are hitting 16MB limit for your document sizes,you may need to split your documents or redesign your databases.

Upvotes: 0

Related Questions