MongoDB is a NoSQL, document-oriented database, meaning it stores data as flexible JSON-like documents rather than in rows and columns like SQL databases.
🔹 Key Concepts
- Database → Collection → Document
- Database: Top-level container (like a SQL database).
- Collection: Group of documents (like a table, but schema-less).
- Document: Individual record stored as BSON (Binary JSON).
Example Document (BSON/JSON-like):
{
"_id": "507f1f77bcf86cd799439011",
"name": "Teekam Singh",
"email": "teekam@example.com",
"roles": ["user", "admin"],
"createdAt": "2025-10-22T10:00:00Z"
}
_idis automatically generated as a unique identifier (ObjectId).- Fields can be added or removed dynamically; no fixed schema required.
🔹 Data Storage Mechanism
- MongoDB stores documents in BSON (Binary JSON) format on disk.
- BSON allows data types not available in JSON (e.g., Date, Binary).
- Documents are grouped into data files on disk managed by the storage engine (default: WiredTiger).
Storage Features:
- Indexes: Speed up queries, can be single-field, compound, or text indexes.
- Replication: Data is replicated across servers for high availability (Replica Set).
- Sharding: Large datasets are split across multiple servers for horizontal scaling.
🔹 Example in Mongo Shell
use myAppDB; // create/use database
db.users.insertOne({
name: "Teekam Singh",
email: "teekam@example.com",
roles: ["user", "admin"],
createdAt: new Date()
});
db.users.find(); // fetch documents
✅ Key Points
- MongoDB stores documents (BSON) inside collections.
- Schema-less: flexible fields per document.
- Supports indexes, replication, and sharding for performance and scalability.
- Ideal for dynamic, hierarchical, or unstructured data.