Database Basics — MongoDB vs Firebase
Databases store your data permanently so it survives server restarts. Two popular choices for Node.js: MongoDB (document database) and Firebase (real-time JSON store).
So far, all our biryani data has been stored in memory — arrays that disappear when the server restarts. In production, you need a database to store data permanently.
Think of it like Swiggy's order system:
- In-memory (what we used) = Writing orders on a paper napkin. If the napkin is thrown away (server restarts), all orders are lost!
- Database = Swiggy's giant digital order book stored on cloud servers. Even if your phone dies, the orders are safe in the cloud.
A database gives you:
- Persistence — Data survives server restarts
- Querying — Search, filter, sort your data efficiently
- Relationships — Connect users to orders to biryani items
- Scalability — Handle millions of records
- Security — Authentication, backups, data integrity
For Node.js developers, the two most popular database choices are MongoDB (NoSQL document database) and Firebase (Google's real-time NoSQL database). Both are NoSQL — meaning they don't use the traditional table/row format of SQL databases.
SQL (Structured Query Language) databases like MySQL, PostgreSQL, and SQLite use tables with rows and columns — like an Excel spreadsheet.
NoSQL databases like MongoDB and Firebase use documents with key-value pairs — like JSON objects.
SQL Table — Biryani Orders:
Table: orders
| id | customer | item | price | status |
|----|-----------|-----------------|-------|-----------|
| 1 | Raju | Chicken Biryani | 250 | delivered |
| 2 | Priya | Mutton Biryani | 350 | cooking |
| 3 | Ali | Veg Biryani | 200 | confirmed |
MongoDB Document — Same Data:
[
{
_id: ObjectId('...'),
customer: 'Raju',
item: 'Chicken Biryani',
price: 250,
status: 'delivered',
orderedAt: ISODate('2025-01-15T10:30:00Z')
},
{
_id: ObjectId('...'),
customer: 'Priya',
item: 'Mutton Biryani',
price: 350,
status: 'cooking'
}
]
Key differences:
| Feature | SQL (PostgreSQL) | NoSQL (MongoDB) |
|---|---|---|
| Structure | Tables with fixed columns | Collections with flexible documents |
| Schema | Rigid — must define column types upfront | Flexible — each document can have different fields |
| Relationships | Foreign keys + JOINs | Embedded docs or references + populate() |
| Scaling | Vertical (bigger server) | Horizontal (more servers) |
| Best for | Complex queries, strict data integrity | Rapid development, flexible data, JSON documents |
For our Biryani API, we'll use MongoDB because it works naturally with JSON (JavaScript objects), has excellent Node.js support via Mongoose, and is easy to get started with.
MongoDB is a NoSQL document database that stores data as BSON (Binary JSON) documents. It's the most popular database for Node.js applications.
Key MongoDB concepts:
- Database — A container for collections (like a restaurant's data folder)
- Collection — A group of documents (like a menu category — Biryanis, Curries)
- Document — A single record stored as JSON (like one biryani item)
- Field — A key-value pair in a document (like "name": "Chicken Biryani")
- _id — A unique identifier automatically added to every document
Sample MongoDB document for a Biryani order:
{
_id: ObjectId('507f1f77bcf86cd799439011'),
customer: 'Raju',
items: [
{ name: 'Chicken Biryani', qty: 2, price: 250 },
{ name: 'Gulab Jamun', qty: 1, price: 60 }
],
total: 560,
address: 'Banjara Hills, Hyderabad',
status: 'delivered',
paymentMethod: 'UPI',
orderedAt: new Date('2025-01-15T10:30:00Z'),
deliveredAt: new Date('2025-01-15T11:15:00Z')
}
Notice how a single document contains all the order data — customer, items, payment, address, status. In SQL, this would require multiple tables (orders, order_items, customers) joined together. MongoDB's document model keeps related data together, making it faster to read.
MongoDB Atlas is the official cloud-hosted MongoDB service. It offers a free tier (512MB storage) perfect for learning and small projects. We'll use Atlas in the next chapter.
Firebase is Google's NoSQL database platform. Unlike MongoDB (which you host yourself or use Atlas), Firebase is a fully managed Backend-as-a-Service (BaaS).
Key Firebase concepts:
- Realtime Database — Stores data as a large JSON tree. Changes sync to all connected clients in real-time
- Firestore — Firebase's newer, more structured database. Uses collections and documents like MongoDB
- No server needed — Firebase includes auth, storage, hosting — you can build an app without any backend code
Sample Firebase Realtime Database structure:
{
"orders": {
"-Nabc123": {
"customer": "Raju",
"item": "Chicken Biryani",
"price": 250,
"status": "delivered"
},
"-Ndef456": {
"customer": "Priya",
"item": "Mutton Biryani",
"price": 350,
"status": "cooking"
}
},
"menu": {
"-Mghi789": {
"name": "Chicken Biryani",
"price": 250
}
}
}
Firebase pros: No server management, real-time sync, built-in auth, great for MVPs. Firebase cons: Vendor lock-in, can get expensive at scale, less query flexibility than MongoDB.
So which database should you choose for your Node.js project? Here's a Swiggy-style comparison:
| Factor | MongoDB (Atlas) | Firebase (Firestore) |
|---|---|---|
| Setup effort | Medium — create Atlas cluster, install mongoose | Low — create project, add SDK, start coding |
| Query power | Excellent — aggregation pipeline, complex queries | Good — limited queries, no JOINs or aggregation |
| Real-time | Manual — need change streams or Socket.io | Built-in — real-time sync out of the box |
| Pricing | Predictable — free tier (512MB), then usage-based | Can spike — pay per read/write/delete operation |
| Self-host option | Yes — host MongoDB on your own server | No — Firebase is cloud-only |
| Node.js support | Excellent — Mongoose ODM, mature drivers | Good — Firebase Admin SDK |
| Best for | Custom Node.js backends, complex APIs | Mobile apps, real-time features, quick prototypes |
For our course, we'll use MongoDB with Mongoose because:
- It's the most popular database for Node.js production apps
- Mongoose gives us schema validation, middleware, and easy querying
- You can host it for free on Atlas or locally
- It's used by Swiggy, Uber, and thousands of production apps
Firebase is great too — especially for mobile apps and real-time features — but MongoDB is the standard for Node.js backend development.
Key Takeaways
- ✅ Databases provide persistence — data survives server restarts unlike in-memory storage.
- ✅ SQL = tables with rows/columns (MySQL, PostgreSQL). NoSQL = documents with JSON (MongoDB, Firebase).
- ✅ MongoDB stores data as BSON documents in collections. Each document has an _id field.
- ✅ Firebase is a fully managed BaaS with real-time sync — great for mobile apps and quick prototypes.
- ✅ MongoDB is more flexible for custom Node.js backends with complex queries.
- ✅ NoSQL does NOT mean no schema — you still need to plan your data structure carefully.
- ✅ For this course: MongoDB with Mongoose — the industry standard for Node.js backend development.
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login