Learn MongoDB and its ecosystem step-by-step --- from zero to production-ready expert.
-
Understand databases, their purpose, and types.
-
Learn how MongoDB fits in the NoSQL world.
-
Grasp JSON, BSON, and MongoDB architecture basics.
A database is an organized collection of data that can be easily accessed, managed, and updated.
| Type | Example | Description |
|---|---|---|
| Relational (SQL) | MySQL, PostgreSQL | Structured, tabular data with fixed schema. |
| Non-relational (NoSQL) | MongoDB, Cassandra | Flexible, schema-less data, optimized for scale. |
MongoDB belongs to the Document-Oriented NoSQL family.
-
Stores data in JSON-like documents (BSON --- Binary JSON).
-
Schema can vary across documents.
-
Horizontal scaling using sharding.
-
High availability via replica sets.
brew tap mongodb/brew brew install mongodb-community@7.0 brew services start mongodb-community@7.0
docker run -d -p 27017:27017 --name mongo mongo:7.0
Verify installation:
mongosh
| SQL Term | MongoDB Term |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
-
Create a new database named
school. -
Create a collection named
students. -
Insert 3 sample student documents:
db.students.insertMany([ { name: "Alice", age: 20, major: "Physics" }, { name: "Bob", age: 21, major: "Math" }, { name: "Charlie", age: 22, major: "CS" } ])
- Fetch all:
db.students.find()
-
Perform Create, Read, Update, Delete operations.
-
Master query filters, projections, and indexes.
db.students.insertOne({ name: "David", age: 23, major: "Biology" }) db.students.insertMany([ { name: "Eve", age: 24, major: "Chemistry" }, { name: "Frank", age: 25, major: "Math" } ])
db.students.find({ major: "Math" })
db.students.find({}, { name: 1, major: 1 })
db.students.find({ age: { $gt: 21 } })
db.students.find({ $or: [{ major: "CS" }, { age: { $lt: 22 } }] })
db.students.updateOne( { name: "Alice" }, { $set: { major: "Data Science" } } ) db.students.updateMany( { major: "Math" }, { $inc: { age: 1 } } )
db.students.deleteOne({ name: "Eve" }) db.students.deleteMany({ age: { $gt: 23 } })
db.students.createIndex({ name: 1 }) db.students.getIndexes()
Check performance:
db.students.find({ name: "Alice" }).explain("executionStats")
-
Insert 10 student documents.
-
Query all students older than 21 sorted by age descending.
-
Create an index on
major. -
Use
.explain()to measure query performance.
-
Learn embedding vs referencing.
-
Design scalable schemas.
-
Use Aggregation Framework.
{ name: "Alice", courses: [ { title: "Math 101", grade: "A" }, { title: "CS 101", grade: "B" } ] }
// students { _id: 1, name: "Alice" } // courses { student_id: 1, title: "Math 101", grade: "A" }
-
Bucket Pattern: Group time-series data.
-
Subset Pattern: Embed frequently accessed subset of data.
-
Extended Reference Pattern: Avoid excessive joins.
Example:
db.students.aggregate([ { $match: { age: { $gte: 20 } } }, { $group: { _id: "$major", avgAge: { $avg: "$age" } } }, { $sort: { avgAge: -1 } } ])
-
Build a
postscollection with user references. -
Find the top 3 users with the most posts.
-
Use
$lookupto join user data.
-
Understand transactions, replication, and sharding.
-
Learn performance and scaling best practices.
session = db.getMongo().startSession() session.startTransaction() students = session.getDatabase("school").students students.updateOne({ name: "Alice" }, { $inc: { age: 1 } }) session.commitTransaction() session.endSession()
-
Replica set = primary + secondaries.
-
Provides failover & read scaling.
Commands:
mongod --replSet rs0 mongosh rs.initiate() rs.status()
- Split data horizontally for scale.
sh.enableSharding("school") sh.shardCollection("school.students", { major: 1 })
mongodump --out /backup mongorestore /backup
-
Use Atlas to deploy, monitor, and scale clusters.
-
Explore Data API, Triggers, and Vector Search.
-
Create free cluster on cloud.mongodb.com
-
Whitelist your IP.
-
Connect via VSCode or driver URI.
'https://data.mongodb-api.com/app/data-abcde/endpoint/data/v1/action/find'\
-H 'Content-Type: application/json'\
-H 'api-key: <YOUR_API_KEY>'\
-d '{"dataSource":"Cluster0","database":"school","collection":"students"}'
For GenAI / RAG:
db.students.createIndex({ embeddings: "vector" }, { vectorDimensions: 768, vectorType: "float32" })
- Connect MongoDB with backend frameworks.
client = MongoClient("mongodb://localhost:27017/")
db = client.school
students = db.students.find()
for s in students:
print(s)
npm install mongoose
mongoose.connect("mongodb://localhost:27017/school");
const Student = mongoose.model("Student", { name: String, age: Number });
await Student.create({ name: "Zara", age: 22 });
Build a Task Manager API with:
-
Express + Mongoose
-
CRUD endpoints
-
Connect to Atlas cluster
- Deploy MongoDB apps, automate backups, visualize data.
services:
mongo:
image: mongo:7.0
ports:
- "27017:27017"
-
Connect to Atlas cluster
-
Build dashboard for user analytics
on:
schedule:
- cron: "0 3 * * *"
jobs:
backup:
runs-on: ubuntu-latest
steps:
- name: Dump DB
run: mongodump --uri ${{ secrets.MONGO_URI }} --out ./backup