MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. This guide will walk you through the basics of working with MongoDB.
1. Installation 🧰
- Linux: Use
sudo apt install mongodb
(Ubuntu/Debian) or download from MongoDB官网 - macOS: Run
brew tap mongodb/brew && brew install mongodb
- Windows: Download the installer from MongoDB Windows Release
- Docker:
docker run -d -p 27017:27017 --name mongodb mongo
2. Core Concepts 🧠
- Database: A container for collections
- Collection: A group of documents (similar to tables in SQL)
- Document: Data stored in key-value pairs (like JSON objects)
- Cursor: Used to iterate over query results
3. Basic Operations 📦
// Insert a document
db.collection.insertOne({ name: "Alice", age: 30 })
// Find documents
db.collection.find().limit(5)
// Update a document
db.collection.updateOne({ name: "Alice" }, { $set: { age: 31 } })
// Delete a document
db.collection.deleteOne({ name: "Alice" })
4. Advanced Features 🚀
- Indexing for faster queries
- Aggregation pipelines for data analysis
- Replication for high availability
5. Practice Now ⚙️
Try MongoDB Playground to experiment with queries and database operations.
For more tutorials, check out our Python Tutorial or Node.js Tutorial.