Databases & System Design•Part 3: NoSQL & Document Databases (MongoDB, DynamoDB) (26 - 40)
When to choose MongoDB over PostgreSQL:
In Simple Words: When schemas vary wildly between records, when data is deeply nested and accessed together, and when horizontal sharding out of the box is required.
javascript
// MongoDB polymorphic schema example (different attributes per item):
db.products.insertMany([
{ name: "Laptop", type: "electronics", cpu: "M3", ram: "16GB" },
{ name: "T-Shirt", type: "clothing", size: "L", material: "Cotton" }
]);