█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/MongoDB/Document vs Relational Databases
30 minBeginner

Document vs Relational Databases

After this lesson, you will be able to: Compare document databases to relational databases honestly: when MongoDB is the right choice and when it isn't.

MongoDB is the most-used document database. Loved + hated. Useful for some shapes of data; the wrong tool for others. This subtrack teaches when to pick it.

This is a free introductory lesson. No purchase required.

Document vs relational

Relational (Postgres, MySQL): structured tables with rows + columns; relationships via foreign keys; SQL queries. Document (MongoDB, DynamoDB, Firestore): collections of JSON-like documents; nested data first-class; query API per platform. Tradeoff: relational gives you schema enforcement + arbitrary joins; document gives you flexible schema + read-fast nested data.

When MongoDB is the right choice

Your data is genuinely document-shaped (CMS articles, product catalogs with varied attributes, IoT events). Read patterns are 'fetch one document + everything related' rather than 'join across many tables'. Schema must flex frequently (rapid prototypes, multi-tenant SaaS where each tenant differs). You need horizontal scale + Mongo Atlas managed service. Time-series data (Mongo has dedicated time-series collections).

When MongoDB is the wrong choice

Your data has lots of cross-relationships (joins), that's what SQL is for. You need strong ACID across multiple documents (Mongo has multi-document transactions but they're an afterthought). Reports + analytics, SQL is genuinely better at ad-hoc reporting. Reaching for MongoDB 'because NoSQL is modern', that's a vibes decision, not a technical one.

Setup options

Two paths.

tsx
# Option 1: MongoDB Atlas (free tier, recommended for learning)
# Sign up at mongodb.com/atlas → create a free M0 cluster → get connection string
# Connect with `mongosh "mongodb+srv://..."`
# Option 2: local MongoDB via Docker
docker run --name mongo -p 27017:27017 -d mongo:7
mongosh "mongodb://localhost:27017"
# Once connected:
test> show dbs
test> use lastwrite
lastwrite> show collections
lastwrite> db.users.insertOne({ name: "Alex", email: "[email protected]" })
lastwrite> db.users.find()

💡 The 'NoSQL means no schema' trap

MongoDB doesn't enforce schemas by default, but your code does. You'll have a de-facto schema; it just lives in your application code instead of the database. Use Mongoose (Node.js) or schema validators to make that schema explicit. Otherwise your DB rots into 'we have 12 fields and which ones are populated depends on when the doc was written'.

Common mistakes only experienced devs catch

Picking MongoDB without checking if data is actually document-shaped. Using MongoDB as a relational DB (lots of joins via $lookup → slow + complex). Skipping schema validation (your DB becomes garbage in 6 months). Storing money as Double instead of Decimal128 (floating-point precision bugs).

Back to MongoDB
MongoDB Fundamentals→