Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of a string type in MongoDB

Tags:

mongodb

I know that ObjectIDs are 12-bytes in MongoDB. And I know that numbers are 64-bit. But how do I find out the size of a saved string

like image 459
Anders Östman Avatar asked Dec 03 '14 14:12

Anders Östman


Video Answer


1 Answers

The bson specification at http://bsonspec.org/spec.html is a good starting point.

In particular, it's important to realize that BSON does have integer types, including 32 bit integers which is even the default, despite the fact that JavaScript uses a 64 bit floating point numbers - MongoDB uses JavaScript a lot, but it's not written in JavaScript and has different types. The existence of true integers is quite important, otherwise a $inc on a large number wouldn't work.

To answer your question: strings are stored as UTF-8 encoded strings with a zero terminator and a 32-bit length in front, plus the 1-byte type indicator and the element name, of course. Keep in mind that objects have additional overhead.

like image 70
mnemosyn Avatar answered Sep 21 '22 05:09

mnemosyn