Bohn
Bohn

Reputation: 26919

To insert into MongoDB: Does the size of document converted to BSON change?

Ok I am new to MongoDB and been reading a book about it, some where it is saying

"Documents larger than 4MB (when converted to BSON ) cannot be saved to database "

Ok...4MB cap for documents? The thing that confused me is the thing they say in parenthesis : ( when converted to BSON ) : So the size is getting changed when converted to BSON?

because a few line later it is saying:

"To give you an idea of how much 4MB is, the entire text of War and Peace is just 3.14 MB "

What? Ok now I am confused. Help Me!

Upvotes: 1

Views: 871

Answers (1)

Derick
Derick

Reputation: 36774

In theory, the BSON file also contains the field names which take up space and some overhead. In general, the overhead is not more than a few bytes. Besides some document overhead (a few bytes), each value stored also has some additional storage bytes. For a simple string it's 6 bytes extra (1 for the type, 4 for the length and 1 for a trailing \0 character). You can see the overhead on the bsonspec.org website. The length of a simple string itself doesn't change "when converted to BSON", but the addition of the length etc makes it a tiny bit larger. Most of the drivers have a helper function to figure out how large a document would be. In PHP you would do:

<?php
$document = array(
    '_id' => new MongoId(),
    'name' => 'Derick'
);
echo strlen(bson_encode($document)), "\n";
?>

Which in this case prints 39.

Also, the current document limit is 16 MB- it has changed since the book was written.

Upvotes: 3

Related Questions