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.
1. What even is MongoDB? π€
Section titled β1. What even is MongoDB? π€β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.
Quick Vocabulary
Section titled βQuick Vocabularyβ| 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 |
A Real Document (in plain JSON)
Section titled βA Real Document (in plain JSON)β{ "_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.
2. Getting It Running on Your Computer π»
Section titled β2. Getting It Running on Your Computer π»β- Download MongoDB Community Server β mongodb.com/try/download/community
- Also grab MongoDB Compass β itβs a friendly visual app so you donβt have to type every command
- Open your terminal and type:
mongoshIf you see a prompt like test>, congratulations β MongoDB is running and talking to you! π
3. Your First Real Actions (CRUD) π οΈ
Section titled β3. Your First Real Actions (CRUD) π οΈβ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
Example: A Simple To-Do App in MongoDB
Section titled βExample: A Simple To-Do App in MongoDBβ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.
5. Aggregation β Asking Smart Questions π
Section titled β5. Aggregation β Asking Smart Questions πβ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 })8. Backups β Your Safety Net πͺ
Section titled β8. Backups β Your Safety Net πͺβ# Take a backupmongodump --db=mydb --out=/backup/2026-07-31
# Restore itmongorestore --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!
9. Security β Locking the Front Door π
Section titled β9. Security β Locking the Front Door πβuse mydatabasedb.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.
10. Keeping an Eye on Things π
Section titled β10. Keeping an Eye on Things πβdb.serverStatus() // "How is my server feeling today?"db.currentOp() // "What is it doing right now?"π Quick Cheat Sheet
Section titled βπ Quick Cheat SheetβConnect
mongosh "mongodb://user:pass@host:27017/dbname"Basics
show dbsuse mydbshow collectionsCRUD
db.col.insertOne({...})db.col.find({...})db.col.updateOne({filter}, {$set:{...}})db.col.deleteOne({...})Indexes
db.col.createIndex({ field: 1 })db.col.getIndexes()Backup
mongodump --db=mydb --out=/backupmongorestore --db=mydb /backup/mydbβοΈ Keep adding your own real-world notes here as you learn β this page grows with you.