Your application always stores the data that is given to it by the user in the memory and then it stores that data to a storage medium device mainly the disk or other type of storage devices, and MongodDB uses memory mapped files as the server cannot stores all its information in the memory instead what it does is it creates a memory mapped array of files and map it using Memory Mapped file.
How are memory-mapped files used? Most commonly by operating system itself. Every time a binary image is loaded into memory, it is actually loaded there as a memory-mapped file. The file you see loaded into a process’s virtual memory address space are actually mapped there as memory-mapped files by operating system.
As you can see in the picture above it shows how memory-mapped file is stored and how MongoDB uses that to make write faster in database, the same process happens when you read the data but in reverse, as this all happens by operating system the Mongo engine will then focus on delivering your data, it’s’ very good idea to leverage the operating system functionality, because its the core functionality of operating system it’s been worked a lot, optimized well and it’s really fast and stable.
Internal Storage.
So let’s talk about how your document or data is saved in other world what is the internal storage of MongoDB and the answer to this is BSON, BSON is a Binary JASON.
There are many advantages in BSON one of the most important of them is that it’s in binary it’s so close to the binary of operating system as a result it’s super-fast in writing and reading the data, another advantage is its also scheme-less.
Some of the key points of BSON are as bellow, these are by no means all the functionality but they are the most highlighted one, and also if you want to get more information about BSON then you can visit their site bsonspec.org
Lightweight
- Keeping spatial overhead to a minimum is important for any data representation format, especially when used over the network.
Traversable
- BSON is designed to be traversed easily. This is a vital property in its role as the primary data representation for MongoDB.
Efficient
- Encoding data to BSON and decoding from BSON can be performed very quickly in most languages due to the use of C data types
How data is stored in MongoDB