πΎ
EPISODE 06
documents Β· collections Β· find Β· aggregation Β· Atlas
MongoDB
Try a NoSQL approach with MongoDB: schemaless documents, flexible queries, aggregation pipelines, and managed hosting on Atlas.
MongoDBNoSQLAtlasaggregation
Duration
β± About 2.5 hours
Level
π Intermediate
Prerequisite
π― db-01
OUTCOME
A working Node app talking to a MongoDB Atlas cluster
What you'll learn
- 1Sign up for MongoDB Atlas (free tier)
- 2Connect from Node with the official driver
- 3Query with find, findOne, insertOne, updateOne, deleteOne
- 4Use the aggregation pipeline for grouping and joining
1. Document Model
json
// A single document
{
"_id": "658a...",
"name": "Alice",
"email": "alice@example.com",
"tags": ["admin", "editor"],
"profile": { "city": "Seoul" }
}No fixed schema β but applying one in code (with Mongoose, Zod, etc.) saves you from data drift.
2. Connect from Node
bash
npm install mongodbjavascript
import { MongoClient } from "mongodb";
const client = new MongoClient(process.env.MONGO_URL);
await client.connect();
const db = client.db("myapp");
const users = db.collection("users");3. CRUD
javascript
// Insert
await users.insertOne({ name: "Alice", email: "a@example.com" });
// Read
const one = await users.findOne({ email: "a@example.com" });
const many = await users.find({ "profile.city": "Seoul" }).limit(10).toArray();
// Update
await users.updateOne({ _id: id }, { $set: { name: "Alicia" } });
await users.updateMany({ active: true }, { $inc: { loginCount: 1 } });
// Delete
await users.deleteOne({ _id: id });4. Aggregation Pipeline
javascript
const result = await db.collection("orders").aggregate([
{ $match: { status: "paid" } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } },
{ $limit: 10 },
]).toArray();Pipelines feed documents through stages β match, group, sort, project, lookup (the NoSQL equivalent of JOIN).
Example code / lecture materials
All lecture materials and example code are openly available on GitHub.
View on GitHub β