Skip to content

MongoDB Administration

πŸƒ MongoDB Administration

From complete scratch to production-ready β€” explained simply

πŸ’‘ Think of this page like your own personal MongoDB notebook. Every section builds on the last β€” start from the top if you’re new, or jump straight to the cheat sheet if you just need a quick reminder.

Imagine a filing cabinet πŸ—„οΈ. In old-school databases (like MySQL), every drawer (table) must hold identical index cards β€” same fields, same order, no exceptions.

MongoDB is more like a box of sticky notes πŸ“. Every note can have different information on it β€” some short, some long, some with extra details β€” and you just toss them all into the same box (called a collection).

🧠 Simple example: A β€œStudents” collection could have one document for a student with just a name and age, and another document for a student that also includes their favorite subjects. No problem β€” MongoDB doesn’t force every document to look the same.

Word you already know (SQL) MongoDB word Simple meaning
Table Collection A folder of related sticky notes
Row Document One sticky note
Column Field One piece of info on the note
Primary Key _id The unique sticker on every note
{
"_id": "abc123",
"name": "Bhaskar",
"hobbies": ["cricket", "coding"],
"isLearning": true
}

That’s it β€” that’s a whole β€œrow” in MongoDB. Simple, readable, no complicated table setup needed.


  1. Download MongoDB Community Server β†’ mongodb.com/try/download/community
  2. Also grab MongoDB Compass β€” it’s a friendly visual app so you don’t have to type every command
  3. Open your terminal and type:
Terminal window
mongosh

If you see a prompt like test>, congratulations β€” MongoDB is running and talking to you! πŸŽ‰


Think of CRUD like managing a to-do list app:

βœ… Create β€” add a new to-do

πŸ” Read β€” check your to-do list

✏️ Update β€” mark a task as done

πŸ—‘οΈ Delete β€” remove a finished task

Add a task (Create)

db.todos.insertOne({ task: "Finish BrainBackup site", done: false })

See all tasks (Read)

db.todos.find()

Mark it done (Update)

db.todos.updateOne(
{ task: "Finish BrainBackup site" },
{ $set: { done: true } }
)

Remove it (Delete)

db.todos.deleteOne({ task: "Finish BrainBackup site" })

That’s genuinely 90% of what you’ll do day-to-day as a beginner. Everything else builds on these four moves.


4. Indexes β€” Your Database’s Table of Contents πŸ“–

Section titled β€œ4. Indexes β€” Your Database’s Table of Contents πŸ“–β€

Imagine searching for one specific recipe in a 1,000-page cookbook with no table of contents β€” you’d have to flip through every page. That’s what MongoDB does without an index (called a β€œcollection scan”).

An index is like adding a table of contents β€” MongoDB jumps straight to the right page.

// "Add a table of contents sorted by name"
db.users.createIndex({ name: 1 })

⚠️ Don’t over-do it: Adding too many indexes is like printing 20 different tables of contents for the same book β€” it takes up space and slows down adding new pages (writes). Only index what you actually search by often.


Aggregation is how you ask MongoDB questions like β€œWho are my top 5 customers this month?” instead of just β€œshow me everything.”

Think of it like a factory assembly line β€” data goes in one end, passes through stations that filter, group, and sort it, and comes out the other end as a clean answer.

db.orders.aggregate([
{ $match: { status: "completed" } }, // Station 1: only completed orders
{ $group: { // Station 2: group by customer
_id: "$customerId",
totalSpent: { $sum: "$amount" }
}},
{ $sort: { totalSpent: -1 } }, // Station 3: highest spenders first
{ $limit: 5 } // Station 4: just the top 5
])

6. Replication β€” Never Put All Your Eggs in One Basket πŸ₯šπŸ§Ί

Section titled β€œ6. Replication β€” Never Put All Your Eggs in One Basket πŸ₯šπŸ§Ίβ€

A Replica Set = keeping identical copies of your data on multiple servers, so if one crashes, another instantly takes over.

🟒 PRIMARY (handles all writes)
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”
πŸ”΅ COPY 1 πŸ”΅ COPY 2
(ready to take over if needed)

βœ… Real talk: This is the difference between β€œour app went down for 3 hours” and β€œour app didn’t even blink” when a server fails.


7. Sharding β€” When One Filing Cabinet Isn’t Enough πŸ—„οΈπŸ—„οΈπŸ—„οΈ

Section titled β€œ7. Sharding β€” When One Filing Cabinet Isn’t Enough πŸ—„οΈπŸ—„οΈπŸ—„οΈβ€

When your data grows too big for one server, sharding splits it across many servers β€” like moving from one filing cabinet to a whole filing room, with a smart receptionist (mongos) directing every request to the right cabinet.

sh.shardCollection("mydb.orders", { customerId: 1 })

Terminal window
# Take a backup
mongodump --db=mydb --out=/backup/2026-07-31
# Restore it
mongorestore --db=mydb /backup/2026-07-31/mydb

🧠 Golden rule: A backup you’ve never tested restoring is basically a backup that doesn’t exist. Test it occasionally!


use mydatabase
db.createUser({
user: "appUser",
pwd: "StrongPassword123",
roles: [ { role: "readWrite", db: "mydatabase" } ]
})

Give each app user only the access it actually needs β€” like giving a house guest a key to the guest room, not the whole house.


db.serverStatus() // "How is my server feeling today?"
db.currentOp() // "What is it doing right now?"

Connect

Terminal window
mongosh "mongodb://user:pass@host:27017/dbname"

Basics

show dbs
use mydb
show collections

CRUD

db.col.insertOne({...})
db.col.find({...})
db.col.updateOne({filter}, {$set:{...}})
db.col.deleteOne({...})

Indexes

db.col.createIndex({ field: 1 })
db.col.getIndexes()

Backup

Terminal window
mongodump --db=mydb --out=/backup
mongorestore --db=mydb /backup/mydb

✍️ Keep adding your own real-world notes here as you learn β€” this page grows with you.