Skip to main content
Database Design·Lesson 4 of 5

NoSQL with MongoDB

MongoDB is the most popular NoSQL database. Instead of tables and rows, it stores data as flexible JSON-like documents in collections. This lesson covers MongoDB's document model, CRUD operations, and schema design patterns.

Documents and Collections

In MongoDB, data is organized into databases, collections, and documents:

Database:   myapp
  Collection: users
    Document: { _id: "...", name: "Alice", email: "a@mail.co" }
    Document: { _id: "...", name: "Bob", email: "b@mail.co" }
  Collection: posts
    Document: { _id: "...", title: "Hello World", author: "Alice" }

A document is a JSON object (technically BSON — Binary JSON). Unlike SQL rows, documents in the same collection can have different fields.

Setting Up

Install the MongoDB Node.js driver or Mongoose (an ODM — Object Document Mapper):

npm install mongoose

Connect to MongoDB:

Ctrl+Enter
HTML
CSS
JS
Preview

Defining Schemas with Mongoose

Although MongoDB is schemaless, Mongoose lets you define schemas for validation and structure:

Ctrl+Enter
HTML
CSS
JS
Preview

CRUD Operations

Create

Ctrl+Enter
HTML
CSS
JS
Preview

Read

Ctrl+Enter
HTML
CSS
JS
Preview

Query Operators

Ctrl+Enter
HTML
CSS
JS
Preview

Operator Reference

OperatorMeaningExample
$eqEqual{ age: { $eq: 25 } }
$gtGreater than{ price: { $gt: 100 } }
$gteGreater than or equal{ score: { $gte: 70 } }
$ltLess than{ stock: { $lt: 5 } }
$inIn array{ role: { $in: ["admin"] } }
$orLogical OR{ $or: [cond1, cond2] }
$andLogical AND{ $and: [cond1, cond2] }
$regexPattern match{ name: { $regex: /^A/i } }

Update

Ctrl+Enter
HTML
CSS
JS
Preview

Delete

await User.findByIdAndDelete(userId);
await User.deleteOne({ email: "old@example.com" });
await User.deleteMany({ isActive: false });

Embedding vs Referencing

The biggest design decision in MongoDB is whether to embed related data or reference it.

Embedding (Denormalized)

Store related data inside the parent document:

Ctrl+Enter
HTML
CSS
JS
Preview

Use embedding when:

  • The related data is always accessed together
  • The embedded array will not grow unboundedly
  • The data does not need to be queried independently

Referencing (Normalized)

Store a reference (ID) to a document in another collection:

Ctrl+Enter
HTML
CSS
JS
Preview

Use referencing when:

  • The related data is large or accessed independently
  • The related data changes frequently
  • You need to query the related data on its own

Aggregation Pipeline

MongoDB's aggregation pipeline lets you process data in stages:

Ctrl+Enter
HTML
CSS
JS
Preview

Practical Exercise

Build a blog schema with MongoDB:

Ctrl+Enter
HTML
CSS
JS
Preview

Key Takeaways

  • MongoDB stores data as flexible JSON-like documents grouped into collections.
  • Mongoose adds schema validation and convenience methods on top of the native driver.
  • Choose embedding for tightly coupled data and referencing for independent, large, or frequently changing data.
  • Query operators like $gt, $in, $or, and $regex give you powerful filtering capabilities.
  • The aggregation pipeline processes data in stages for complex analytics queries.