What is MongoDB ?

MongoDB is a popular NoSQL database that stores data in a flexible, JSON-like format. It is widely used for modern web applications due to its high performance, scalability, and ability to handle unstructured data.

Unlike traditional relational databases (like MySQL or PostgreSQL), MongoDB does not store data in tables and rows. Instead, it stores data as documents inside collections.

Understanding JSON and BSON

MongoDB stores data in BSON format, which stands for Binary JSON (JavaScript Object Notation).

  • JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy to read and write for both humans and machines.

  • BSON is an extension of JSON that includes additional data types (such as Date, Binary, etc.) and is optimized for speed and space.

Example JSON Document:

sh
{
“name”: “Vishal”
}

This document represents a user and is an example of what MongoDB stores internally.

Documents and Collections

  • A document in MongoDB is a single record in JSON/BSON format.

  • A collection is a group of related documents, similar to a table in relational databases.

Example:

Suppose we are building a social media app:

  • We might have a users collection where each document represents a user.

  • We might also have a posts collection where each document represents a user’s post.

Sample User Documents:

sh
{ “name”: “Vishal” }

sh
{ “first_name”: “Vishal” }

As you can see, MongoDB allows different fields in different documents, even within the same collection.

Schema-less Nature of MongoDB

MongoDB is a schema-less database, which means:

  • There is no fixed structure for documents.

  • Each document in a collection can have different fields.

  • You can add new fields anytime without modifying existing documents.

While this flexibility is powerful, it can lead to inconsistent data if not managed properly.

Why Use a Schema Design in MongoDB?

Even though MongoDB is schema-less, it’s a best practice to define a consistent structure (schema) for your documents. This helps:

  • Maintain data integrity

  • Improve application logic

  • Prevent bugs and unexpected behavior

You can use libraries like Mongoose (in Node.js) to define schemas and models, enforcing structure and validation on your MongoDB documents.

Key Takeaways

  • MongoDB stores data as flexible, JSON-like documents.

  • BSON is the binary representation used internally for storage.

  • It is schema-less by default, meaning documents in the same collection can have different fields.

  • For production applications, it’s recommended to enforce structure using schema definitions or ODMs like Mongoose.

 

Scroll to Top