What is MongoDB and how is it different from MySQL or PostgreSQL?
Short answer
MongoDB is a NoSQL document database. Instead of tables with fixed columns and rows, it stores data as flexible JSON-like documents grouped into collections. Each document can have different fields.
Explained in simple words
- In MySQL/PostgreSQL, data is like an Excel sheet: every row has the same columns, and you must define them first.
- In MongoDB, data is like a folder of JSON files: each file (document) can have its own fields, and you can nest data inside it.
The same customer, both ways
SQL table customers:
| id | name | email | city |
| 1 | Aditya | [email protected] | Gurugram |
MongoDB document in collection customers:
{
"_id": "6745e1f2a1b2c3d4e5f60789",
"name": "Aditya",
"email": "[email protected]",
"address": { "city": "Gurugram", "pin": "122001" },
"tags": ["premium", "early-adopter"]
}
Terms side by side
| SQL | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
| JOIN | Embedded documents or $lookup |
| SQL queries | Query methods like find() |
When companies choose MongoDB
- Data whose shape changes often (product catalogues, user profiles, content).
- Apps built in JavaScript/Node.js that already work with JSON.
- Very large scale, using sharding to spread data across servers.
Relational databases are still better when you need strict structure, complex joins and strong transactions across many tables — like banking or accounting.
What to say in the interview
"MongoDB is a document database. Data is stored as JSON-like documents in collections, with a flexible schema, so documents can have different fields and nested objects. Compared with MySQL or PostgreSQL, there are no fixed tables or joins by default. It suits fast-changing data and JSON-heavy apps; relational databases suit structured data with complex relationships and transactions. The default port is 27017."
Follow-up questions you may get
- What is
_id? The unique ID of every document, created automatically if you don't set one. - What is a replica set? A group of MongoDB servers with the same data — one primary and secondaries — for high availability.