When would you choose MongoDB over PostgreSQL, and vice versa?
IntermediateConceptMongoDB
Answer
Short answer
Choose MongoDB when data is document-shaped and varies a lot, you read whole objects at once, and you need easy horizontal scaling. Choose PostgreSQL when you have many relationships, need complex queries, joins and strong multi-row transactions, or strict schemas. Both are excellent; the data model and query patterns decide.
Comparison
| Need | MongoDB | PostgreSQL |
|---|---|---|
| Flexible / evolving schema | ✅ natural | ✅ via JSONB, but less native |
| Complex joins and reporting | ⚠️ $lookup, aggregation |
✅ excellent |
| Multi-document/row transactions | ✅ supported (with overhead) | ✅ core strength |
| Built-in horizontal sharding | ✅ native | ⚠️ needs Citus or app-level |
| Strict constraints (FKs, checks) | ⚠️ limited (schema validation) | ✅ |
| Typical use | Catalogs, content, IoT, user profiles | Orders, payments, accounting, most business apps |
Honest note
PostgreSQL's JSONB covers many "flexible data" cases, so many teams default to Postgres and add MongoDB when its document model or sharding clearly fits.
What to say in the interview
"It depends on data shape and access patterns: MongoDB for varied documents read as a whole and native scale-out; PostgreSQL for relational data, complex queries and strict integrity. Postgres JSONB narrows the gap, so I'd choose based on the dominant query pattern."