Paramclasses Academy - Blog

Tutorials Details

←
Post 4 of 35
→

How MongoDB Stores Data: BSON, JSON and Document Structure

How MongoDB Stores Data: BSON, JSON and Document Structure

November 5, 2026

❖ Welcome to Param Classes!

When you work with a database, understanding how information is stored is just as important as knowing how to insert or retrieve it. MongoDB is a NoSQL document database that stores information in a flexible, JSON-like structure.

But have you ever wondered how MongoDB actually stores these documents? What is BSON? Is BSON the same as JSON? Why does MongoDB use BSON instead of storing ordinary JSON text?

In this tutorial, we will understand MongoDB data storage, JSON, BSON, document structure, data types, nested documents and arrays with practical examples.

✦ 1. How Does MongoDB Store Data?

MongoDB stores information as BSON documents inside collections.

BSON stands for Binary JSON. It is a binary-encoded serialization format that extends the JSON data model with additional data types.

The basic MongoDB storage structure looks like this:

MongoDB Server
      |
      |-- Database
              |
              |-- Collection
                      |
                      |-- BSON Document 1
                      |-- BSON Document 2
                      |-- BSON Document 3

For example, a student management application may have a database named school_management and a collection named students.

Each student record is stored as a separate document.

✦ 2. What Is JSON?

JSON stands for JavaScript Object Notation. It is a lightweight text-based format used to represent and exchange data between applications.

JSON is easy to read and understand. It is commonly used in REST APIs, web applications and mobile application development.

❖ Example of JSON Data

{
  "name": "Rahul Kumar",
  "age": 22,
  "course": "BCA",
  "city": "Raipur"
}

Let's understand this example:

  • name is a string.

  • age is a number.

  • course is a string.

  • city is a string.

The information is represented using key-value pairs.

❖ Where Is JSON Used?

JSON is commonly used for:

  • Sending data between frontend and backend.

  • REST API requests and responses.

  • Web application communication.

  • Mobile application data exchange.

  • Configuration files.

For example, a Flutter or React application can receive student information from a backend API in JSON format.

✦ 3. What Is BSON in MongoDB?

BSON stands for Binary JSON. It is the format MongoDB uses to represent documents in its storage and communication systems.

Although BSON is based on the JSON data model, it supports additional data types that ordinary JSON does not represent directly.

Important BSON data types include:

  • String

  • Double

  • Int32

  • Int64

  • Boolean

  • Date

  • ObjectId

  • Array

  • Embedded Document

  • Binary Data

  • Decimal128

  • Null

For example, MongoDB can store an ObjectId and a Date as their proper BSON types instead of treating them as ordinary strings.

❖ Example of a MongoDB Document

{
  _id: ObjectId("507f1f77bcf86cd799439011"),
  name: "Rahul Kumar",
  age: 22,
  active: true,
  admissionDate: ISODate("2026-01-15T00:00:00Z")
}

This example demonstrates different MongoDB data types:

  • _id uses ObjectId.

  • name uses String.

  • age uses a numeric value.

  • active uses Boolean.

  • admissionDate uses Date.

The MongoDB shell can display values using convenient representations such as ObjectId() and ISODate().

✦ 4. Difference Between JSON and BSON

JSON and BSON are related, but they are not identical.

Feature JSON BSON
Full Form JavaScript Object Notation Binary JSON
Format Text-based Binary-encoded
Readability Human-readable Not directly human-readable
Data Types Basic JSON types Additional data types
Date Usually represented as a string Has a Date type
ObjectId No native ObjectId type Supports ObjectId
Main Use Data exchange MongoDB document representation

Important: BSON is not simply compressed JSON. It is a separate binary serialization format with its own encoding rules and data types.

✦ 5. Understanding MongoDB Document Structure

A MongoDB document contains fields and values.

Each field has a name and an associated value.

For example:

{
  name: "Amit Sharma",
  age: 21,
  course: "Flutter",
  city: "Bilaspur"
}

Here:

  • name is a field.

  • "Amit Sharma" is its value.

  • age is a field.

  • 21 is its value.

MongoDB documents can contain strings, numbers, Boolean values, arrays and nested documents.

❖ Example: Student Document with Multiple Data Types

{
  name: "Priya Singh",
  age: 23,
  feesPaid: true,
  percentage: 85.5,
  course: "Python",
  admissionDate: new Date(),
  skills: ["Python", "HTML", "MongoDB"]
}

This document stores different types of information about a student.

✦ 6. Important BSON Data Types with Examples

Let's understand some commonly used BSON data types.

❖ 1. String

Used to store text.

{
  name: "Rahul Kumar"
}

❖ 2. Number

MongoDB supports numeric types such as Int32, Int64, Double and Decimal128.

{
  age: 22,
  percentage: 85.5
}

The exact BSON numeric type depends on the value and how it is represented or inserted.

❖ 3. Boolean

Used to store true or false values.

{
  isActive: true
}

❖ 4. Date

Used to store date and time information.

{
  admissionDate: new Date()
}

MongoDB BSON Date represents a UTC datetime value with millisecond precision.

❖ 5. ObjectId

MongoDB commonly uses ObjectId as the default value for automatically generated _id fields.

{
  _id: ObjectId()
}

❖ 6. Array

An array stores multiple values in a single field.

{
  skills: ["HTML", "CSS", "JavaScript"]
}

❖ 7. Embedded Document

An embedded document stores another object inside a document.

{
  name: "Amit",
  address: {
    city: "Raipur",
    state: "Chhattisgarh"
  }
}

✦ 7. How to Insert Different Data Types in MongoDB

Now let's perform a practical example using MongoDB Shell.

First, select your database:

use school_management

Insert a student document:

db.students.insertOne({
  name: "Rahul Kumar",
  age: 22,
  course: "BCA",
  feesPaid: true,
  percentage: 82.5,
  admissionDate: new Date(),
  skills: ["HTML", "CSS", "JavaScript"],
  address: {
    city: "Raipur",
    state: "Chhattisgarh"
  }
})

This command inserts a document containing strings, numbers, Boolean values, dates, arrays and a nested document.

To display the stored information:

db.students.find()

To display the result in a readable format:

db.students.find().pretty()

The pretty() method formats the output for easier reading in mongosh.

✦ 8. What Are Nested Documents and Arrays?

MongoDB allows developers to store related information inside a single document using nested documents and arrays.

For example, a student document may include address details:

{
  name: "Rahul",
  address: {
    city: "Raipur",
    state: "Chhattisgarh",
    pincode: 492001
  }
}

It may also contain multiple skills:

{
  name: "Rahul",
  skills: [
    "PHP",
    "JavaScript",
    "MongoDB",
    "Node.js"
  ]
}

This structure can make related information easier to retrieve together.

However, deeply nested documents and very large arrays should be designed carefully. Document size, update patterns and application queries matter when choosing a data structure.

✦ 9. Important MongoDB Document Rules

While designing MongoDB documents, remember these important points:

  1. Every document must have a unique _id within its collection.

  2. MongoDB automatically generates an ObjectId if _id is not provided.

  3. Documents can contain nested objects and arrays.

  4. Documents in a collection can have different fields.

  5. MongoDB documents have a maximum BSON document size of 16 MiB.

  6. Field names should be meaningful and consistent.

  7. Use appropriate data types for dates, numbers and Boolean values.

These practices help you design better database structures.

✦ 10. Why Should Developers Understand BSON and JSON?

Understanding JSON and BSON is important when developing applications with MongoDB.

It helps developers:

  • Understand how MongoDB represents information.

  • Work with REST API data.

  • Select suitable BSON data types.

  • Store nested objects and arrays.

  • Connect MongoDB with Node.js, Python and PHP.

  • Design efficient database documents.

  • Troubleshoot data conversion problems.

For example, when a Node.js application sends a JSON object to MongoDB, the MongoDB driver converts it into the appropriate BSON representation.

❖ Conclusion

MongoDB stores information using BSON documents organized inside collections. JSON provides a readable format for representing and exchanging data, while BSON provides MongoDB with a binary-encoded document representation and additional data types.

In this tutorial, we learned how MongoDB stores data, the difference between JSON and BSON, important BSON data types, nested documents and arrays, and how to insert practical student records.

At Param Classes, we encourage students to practice every database concept using real examples. Create your own collection, insert documents and explore different data types to strengthen your MongoDB skills.

Learn Today, Grow Tomorrow!

←
Post 4 of 35
→