MongoDB is a document-oriented NoSQL database that stores data in JSON format (BSON) within flexible collections.
🤔 Why MongoDB?
MongoDB is for:
- 📄 JSON data - Store JSON directly like in JavaScript
- 🚀 Rapid development - No need to create tables in advance
- 🔄 Flexible structure - Data can change shape
- ⚡ Simple - Closer to programming
Use it for:
- REST APIs (JSON data)
- Mobile applications
- Storing logs, events
- Quick prototypes
- When data doesn’t fit in a spreadsheet
🚀 Installation
| 📌 System | 🧠 Command |
|---|---|
| 🐧 Ubuntu/Debian | sudo apt install mongodb-server mongodb-clients |
| 🎩 CentOS/RHEL | sudo yum install mongodb-org |
| 🍎 macOS (Homebrew) | brew install mongodb-community |
| 🐳 Docker | docker run -d mongo:7 |
| 🏢 Official version | mongodb.com/try/download |
⚙️ Basic Configuration
| 📌 Action | 🧠 Command |
|---|---|
| ▶️ Start MongoDB | sudo systemctl start mongod |
| 🔄 Enable at boot | sudo systemctl enable mongod |
| 🔗 Connect | mongosh (or mongo) |
| 📋 View databases | show dbs |
| 🔗 Use a database | use myapp |
| 🚪 Exit mongosh | quit() (or Ctrl+C) |
🗄️ Database Management
| 📌 Action | 🧠 Command |
|---|---|
| 📋 List databases | show dbs |
| 🆕 Create/use a database | use myapp |
| 📊 Current database | db.getName() |
| 🗑️ Delete current database | db.dropDatabase() |
| 📊 Database statistics | db.stats() |
| 💾 Database size | db.stats().dataSize |
📋 Collection Management
| 📌 Action | 🧠 Command |
|---|---|
| 📋 List collections | show collections |
| 🆕 Create a collection | db.createCollection("users") |
| 🗑️ Delete a collection | db.users.drop() |
| 📊 Collection statistics | db.users.stats() |
| 📖 Count documents | db.users.countDocuments() |
| 🔍 First document | db.users.findOne() |
👥 User Management
| 📌 Action | 🧠 Command |
|---|---|
| 👤 Create an admin | db.createUser({user:"admin", pwd:"password", roles:["root"]}) |
| 👤 Create read-only user | db.createUser({user:"reader", pwd:"pwd", roles:["read"]}) |
| 👤 Create read/write user | db.createUser({user:"editor", pwd:"pwd", roles:["readWrite"]}) |
| 📋 List users | db.getUsers() |
| 🗑️ Delete a user | db.dropUser("reader") |
| 🔐 Enable authentication | Edit /etc/mongod.conf: security.authorization: enabled |
💾 Backup and Restore
| 📌 Action | 🧠 Command |
|---|---|
| 💾 Backup a database | mongodump --db myapp --out ./backup/ |
| 💾 Backup everything | mongodump --out ./backup/ |
| 📥 Restore a database | mongorestore --db myapp ./backup/myapp/ |
| 📥 Restore everything | mongorestore ./backup/ |
| 🗜️ Export JSON | mongoexport --db myapp --collection users --out users.json |
| 📦 Import JSON | mongoimport --db myapp --collection users --file users.json |
🔍 Monitoring and Debug
| 📌 Action | 🧠 Command |
|---|---|
| 📊 Server status | db.serverStatus() |
| 🔗 Active connections | db.serverStatus().connections |
| 🐌 Profiler (slow queries) | db.setProfilingLevel(2) |
| 📈 View profile | db.system.profile.find() |
| 💾 Memory usage | db.serverStatus().mem |
| 🔧 Current operations | db.currentOp() |