MongoDB Database Model
MongoDB is a NoSQL database that follows a document-oriented model
instead of the traditional relational model used by RDBMS. This provides
flexibility, scalability, and high performance.
1. Basic Structure of MongoDB
MongoDB organizes data into the following hierarchy:
1. Database → Contains multiple collections.
2. Collection → Contains multiple documents.
3. Document → A single record stored in JSON/BSON format.
4. Field → A key-value pair inside a document.
Example Structure:
Pgsql (PostgreSQL also known as Postgres, is a free and open-source relational database
management system emphasizing extensibility and SQL compliance.)
Database → "ecommerceDB"
├── Collection → "products"
├── Document → { "_id": 1, "name": "Laptop", "price": 1000, "category":
"Electronics" }
├── Document → { "_id": 2, "name": "Phone", "price": 500, "category":
"Electronics" }
2. Core Components of MongoDB Database Model
a) Database
A MongoDB instance can have multiple databases.
Example:
shell
use ecommerceDB
b) Collections
A collection is like a table in RDBMS but without a fixed schema.
Each document inside a collection can have different fields.
Example:
shell
db.createCollection("products")
c) Documents
A document is the primary unit of data in MongoDB.
Stored in JSON (JavaScript Object Notation) or BSON (Binary JSON).
Documents can be nested, meaning they can contain other documents
inside.
Example of a MongoDB document:
json
{
"_id": 1,
"name": "Laptop",
"price": 1000,
"category": "Electronics",
"specs": {
"brand": "Dell",
"RAM": "16GB",
"Storage": "512GB SSD"
}
}
3. MongoDB Schema Design Patterns
Unlike relational databases, MongoDB allows flexible schema designs based on
application needs. Below are common schema models:
a) Embedded/Nested Documents (Denormalization)
Stores related data inside the same document.
Reduces the need for joins and improves read performance.
Example:
json
{
"_id": 1,
"name": "John Doe",
"orders": [
{ "order_id": 101, "total": 250 },
{ "order_id": 102, "total": 150 }
]
}
✅ Best for: When the data is accessed together frequently (e.g., user profiles
with orders).
b) Referenced Documents (Normalization)
Instead of storing data inside the same document, references (foreign
keys) are used.
Useful for reducing document size and handling large datasets.
Example:
Users Collection
json
{
"_id": 1,
"name": "John Doe",
"orders": [101, 102]
}
Orders Collection
json
{
"_id": 101,
"user_id": 1,
"total": 250
}
✅ Best for: When data is updated frequently or has one-to-many
relationships (e.g., users and orders).
4. Indexing in MongoDB
Indexes improve query performance by reducing the time required to find data.
Default Index (_id field)
Every document in MongoDB has a unique _id field that acts as the primary key.
shell
db.products.find({ "_id": 1 })
Custom Index
Creating an index on the name field:
shell
db.products.createIndex({ "name": 1 })
✅ Indexing improves query performance but uses more disk space.
5. Relationships in MongoDB
Unlike RDBMS, MongoDB does not support joins by default. However,
relationships can still be handled.
One-to-One Relationship
Example: A user and their profile.
json
{
"_id": 1,
"name": "Alice",
"profile": {
"email": "[email protected]",
"age": 30
}
}
One-to-Many Relationship
Example: A user with multiple orders (embedded documents).
json
{
"_id": 1,
"name": "John",
"orders": [
{ "order_id": 101, "total": 200 },
{ "order_id": 102, "total": 300 }
]
}
Many-to-Many Relationship
Example: Authors and books.
Authors Collection
json
{
"_id": 1,
"name": "J.K. Rowling",
"books": [101, 102]
}
Books Collection
json
{
"_id": 101,
"title": "Harry Potter",
"authors": [1, 2]
}
📌 Denormalization (embedding) is preferred if the data is read together
frequently.
📌 Normalization (referencing) is better for updating data independently.
6. Aggregation Framework
Aggregation allows data transformation and analysis.
Example: Find total sales for each product category
shell
db.orders.aggregate([
{ "$group": { "_id": "$category", "total_sales": { "$sum": "$price" } } }
])
Aggregation stages:
1. $match – Filters documents.
2. $group – Groups documents.
3. $sort – Sorts results.
4. $project – Modifies output structure.
✅ Aggregation helps in performing complex queries efficiently.
7. Sharding & Replication
Replication
MongoDB supports replica sets, ensuring high availability.
shell
rs.initiate()
✅ If one server fails, another replica takes over automatically.
Sharding
MongoDB distributes data across multiple servers using sharding.
shell
sh.enableSharding("ecommerceDB")
✅ Used for large-scale applications like e-commerce and social media
platforms.
8. Transactions in MongoDB
MongoDB supports multi-document ACID transactions (introduced in version
4.0).
shell
session = db.getMongo().startSession()
session.startTransaction()
db.products.updateOne({ "_id": 1 }, { "$inc": { "stock": -1 } })
session.commitTransaction()
✅ Useful for banking, financial applications, and inventory management.
9. Pros & Cons of MongoDB Database Model
Pros Cons
Flexible schema (no need for predefined
No built-in joins (requires $lookup).
structure).
High scalability (horizontal scaling with Higher memory usage due to
sharding). denormalization.
Faster read/write operations for large ACID transactions are not as strong as
datasets. RDBMS.
Supports rich JSON/BSON queries. Indexing requires careful optimization.
10. When to Use MongoDB?
✅ Best for:
Big data applications.
Real-time analytics.
IoT (Internet of Things).
Social media, recommendation engines.
E-commerce product catalogs.
❌ Not Ideal for:
Applications requiring strict ACID compliance (e.g., banking).
Complex queries with heavy joins.
Final Thoughts
MongoDB's document-oriented model provides flexibility, scalability, and
high performance, making it a powerful choice for modern web applications.
However, it requires careful schema design to balance performance and
maintainability.