What is MongoDB?
What is MongoDB?
MongoDB stores records as documents grouped into collections. A flexible structure still needs deliberate field names, data types and validation.
MongoDB is a document-oriented NoSQL database used for high-volume data storage. Instead of using tables and rows like traditional relational databases (SQL), MongoDB uses collections and documents.
Each document in MongoDB is a JSON-like object called BSON (Binary JSON), which allows for embedded data and flexible schemas.
By the end of this lesson
Describe how a document differs from a table row, and choose a suitable model for a user profile.
Why learn MongoDB?
MongoDB is widely used in modern applications due to its:
- Flexible schema — no need to define tables up front
- Scalability — great for big data and horizontal scaling
- JSON-like structure — naturally maps to code objects (JavaScript, Python, etc.)
SQL vs NoSQL — The Difference
| SQL (Relational) | NoSQL (MongoDB) |
|---|---|
| Uses tables and rows | Uses collections and documents |
| Declared schema; changes are possible | Flexible schema; validation is available |
| Relationships using joins | Embedded documents or references |
| Good for structured data | Great for semi-structured or unstructured data |
Intuition Check
Q: If you’re building an application where different users have different fields in their profile, would SQL or MongoDB be easier to work with?
A: MongoDB! Because you don’t need to predefine a schema. One document can have a name and age, another might have name, age, and hobbies — and that’s totally okay.
Example 1: SQL Table vs MongoDB Document
In SQL:
CREATE TABLE users (
id INT,
name VARCHAR(50),
age INT
);
INSERT INTO users VALUES (1, 'Alice', 25);
In MongoDB:
db.users.insertOne({
_id: 1,
name: "Alice",
age: 25
});
{
acknowledged: true,
insertedId: 1
}
Explanation: MongoDB stores data as documents in collections. A collection can accept this document without a predefined validator. Decide which fields your application requires; flexible storage does not guarantee that every query will be fast.
Example 2: Flexible Schema in Action
You can insert different shaped documents into the same collection:
db.users.insertMany([
{ name: "Bob", age: 30 },
{ name: "Charlie", hobbies: ["chess", "cycling"], location: "India" }
]);
{
acknowledged: true,
insertedIds: [ObjectId("..."), ObjectId("...")]
}
Explanation: Notice how the second document has completely different fields? MongoDB allows that flexibility, which is powerful in agile development environments.
Intuition Check
Q: Is it possible to store nested objects or arrays in SQL tables directly?
A: Many relational databases support JSON values and arrays too. Separate tables and joins remain useful when related records need independent management. MongoDB can directly store nested arrays and objects. This is useful for storing things like a user's multiple addresses or product reviews inside a single document.
Example 3: Embedded Document
db.products.insertOne({
name: "Laptop",
price: 70000,
specs: {
brand: "Dell",
ram: "16GB",
storage: "512GB SSD"
}
});
{
acknowledged: true,
insertedId: ObjectId("...")
}
Explanation: The specs field is a sub-document (nested object). MongoDB handles this natively without additional joins or relations.
Check your understanding
What to remember
MongoDB stores records as documents grouped into collections. A flexible structure still needs deliberate field names, data types and validation.
Key Terms Recap
- Document – A single record (like a row), stored in BSON format
- Collection – A group of documents (like a table)
- BSON – Binary JSON, supports more data types than JSON
Summary
MongoDB is a flexible, document-based NoSQL database perfect for modern applications with semi-structured data. It allows fast development, easy scaling, and natural data modeling using embedded documents and arrays.
Reaching the recap and completing the quizzes are separate milestones.