I am using MongoDB in my project. And I have some questions about how Mongo storage allocated.

For example, I have 16MB full data block, and I want to add new document into this data block. - How MongoDB allocate new data block ? - Will MongoDB move all data or only new document into a new data block ?


The maximum BSON document size is 16 megabytes but to store documents larger than the maximum size MongoDB uses GridFS

as stated in other answer about size of the first file 64MB and size of new datafile that keep on getting double till the size of newly created file reaches to 2GB. Have a look at Mongo db data file

enter image description here


Data files are allocated in advance. The following is slightly simplified, for the sake of shortness.

When a MongoDB database is created, an empty datafile with a size of 64 MB is created. As soon as data is written to this pristine data file, a background process creates a new datafile in order to have one ready of the first datafile is full. This will be repeated every time a document is written to a pristine data file. For each new data file, the file size is doubled until the size to allocate reaches 2GB. From that point on, all new datafiles will have 2GB in size.

As for the data, this is a bit more complicated. Put simply, documents are never moved unless it is really necessary, for example if the document grows in size and exceeds its padding space. So if one of your data files is full and a new document is added, only the new document will be written to the new and formerly pristine data file (triggering the allocation of a new data file), and the old documents stay where they were.

  • Note that this description only applies to the default MMAP storage engine (MongoDB 3.0 and earlier). Optional storage engines in MongoDB 3.0+ (eg. WiredTiger) have different allocation strategies and some considerations such as document padding may not be applicable. – Stennie Apr 21 '15 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.