Ayush Parui
Ayush Parui

Ayush Parui

MongoDB : How Does MongoDB store DATA

MongoDB : How Does MongoDB store DATA

Ayush Parui's photo
Ayush Parui
·Jun 27, 2022·

2 min read

MongoDB use documents to store DATA. What are Documents ? Documents are nothing but a way to organize and store data as set of field-value pairs. For eg:

{
    name: "Apple",   <- Field: Value
    rating: 8,            <- Field: Value
    review: "Nice Fruit"    <- Field: Value
}

But, the question is how MongoDB stores DATA?

When we view or update any Documents in MongoShell it's in the JSON format, which stands for JAVASCRIPT STANDARD OBJECT NOTATION.

JSON Format

Start and end with curly Braces { }

Separate each key and value with a colon :

Separate each key : value pair with a comma ,

keys must be surrounded with quotation marks " "

In MongoDB "keys" are called "fields"

{
  "name": "ejs",
  "version": "1.0.0",
  "lockfileVersion": 2,
  "requires": true,
  "packages": {
    "": {
      "name": "ejs",
      "version": "1.0.0",
      "license": "ISC",
      "dependencies": {
        "body-parser": "^1.20.0",
        "ejs": "^3.1.8",
        "express": "^4.18.1"
      }
    }}

Pros & Cons of JSON

Pros of JSON

  • User-Friendly

  • Readable

  • Familiar for any Developers

Cons of JSON

  • Text-Based format (Text parsing is extremely slow)

Example - Parsing JSON

Imagine we received this text from a web server:

<script>
const txt = '{"name":"John", "age":30, "city":"New York"}'
const obj = JSON.parse(txt);
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age + "," + obj.city;
</script>

Output:

John, 30, New York
  • Space-Consuming (Concern for the Database)

  • JSON supports only limited number of basic Data Types

BSON

MongoDB decided to overcome all the Drawbacks by JSON

Here is what your data will look like if you examine it as it appears in the memory:

Screenshot (761).png

Formats such as this one are called Binary JSON, or BSON.

But, Why BSON ?

Well, the main focus was to Store data in BSON format to bridge the gap between binary format and BSON representation.

It's Optimized for:

  • Speed

  • Space

  • Flexibility

  • High Performance

JSON VS BSON

JSON.png

Here's a small overview on JSON and BSON. All the credit goes to MongoDB University.

LinkedIn

Twitter

Github

Hash Node

 
Share this