This repository contains MongoDB Playground scripts tested in VS Code.
It helps you learn MongoDB step by step with examples and explanations.
- What is MongoDB?
- Key Components
- Installing MongoDB
- Basic Mongo Shell Commands
- CRUD Operations
- Aggregation Framework
- Indexing
- How to Run
- MongoDB is a NoSQL database.
- It stores data in JSON-like documents (actually BSON).
- Unlike SQL databases, MongoDB does not use tables and rows, but collections and documents.
Example Document:
{
"name": "Alice",
"age": 25,
"city": "Delhi"
}Explanation:
- Each document is like a record.
- A collection is like a table.
| Component | Description |
|---|---|
mongod |
Mongo Daemon is mongoDB server engine. Stores data, handles queries, indexes, and connections. Usually runs automatically in background as a service in modern setups. |
mongosh |
MongoDB shell. Command-line interface to run queries. |
| MongoDB Compass | GUI tool to visualize data, run queries, and analyze performance. Connects to mongod automatically. |
💡 Tip: Beginners can start with MongoDB Compass to see documents visually.
- Go to MongoDB Community Download.
- Download Community Edition for your OS.
- Install following instructions.
- Start the server using:
mongod- Open a new terminal for the shell:
mongosh| Command | Explanation | Example |
|---|---|---|
show dbs |
Lists all databases | show dbs |
use <dbName> |
Switch or create a database | use ecommerce |
show collections |
Lists collections in current DB | show collections |
db.dropDatabase() |
Deletes current database | db.dropDatabase() |
CRUD = Create, Read, Update, Delete
// Insert one document
db.users.insertOne({
name: "Alice",
age: 25,
city: "Delhi",
});
// Insert many documents
db.users.insertMany([
{ name: "Bob", age: 28, city: "Mumbai" },
{ name: "Charlie", age: 30, city: "Bangalore" },
]);Explanation:
insertOneadds one document.insertManyadds multiple documents at once.
Expected Output:
{
"acknowledged": true,
"insertedId": ObjectId("...")
}// Find all documents
db.users.find();
// Find users in Delhi
db.users.find({ city: "Delhi" });
// Find specific fields
db.users.find({}, { name: 1, city: 1 });
// Comparison operators
db.users.find({ age: { $gt: 25 } }); // age > 25
db.users.find({ age: { $lte: 30 } }); // age <= 30Explanation:
{ city: "Delhi" }is a filter.{ name: 1, city: 1 }shows onlynameandcityfields.$gt= greater than,$lte= less than or equal.
Expected Output:
[
{ "name": "Bob", "city": "Mumbai" },
{ "name": "Charlie", "city": "Bangalore" }
]// Update one document
db.users.updateOne({ name: "Alice" }, { $set: { city: "Noida" } });
// Update multiple documents
db.users.updateMany({ city: "Mumbai" }, { $set: { verified: true } });Explanation:
$setchanges or adds fields.updateOneupdates the first matching document.updateManyupdates all matching documents.
Expected Output:
{
"acknowledged": true,
"modifiedCount": 1
}// Delete one
db.users.deleteOne({ name: "Charlie" });
// Delete many
db.users.deleteMany({ verified: false });Explanation:
- Deletes documents matching the filter.
Expected Output:
{ "acknowledged": true, "deletedCount": 1 }Used for advanced data analysis (like SQL GROUP BY).
db.orders.aggregate([
{ $match: { status: "delivered" } },
{
$group: {
_id: "$customerId",
totalAmount: { $sum: "$amount" },
orderCount: { $sum: 1 },
},
},
{ $sort: { totalAmount: -1 } },
]);Explanation:
$matchfilters documents.$groupgroups by customerId and calculates totals.$sortsorts by totalAmount descending.
// Single-field index
db.users.createIndex({ name: 1 });
// Compound index
db.users.createIndex({ city: 1, age: -1 });
// List indexes
db.users.getIndexes();
// Drop an index
db.users.dropIndex("name_1");Tip: Use indexes on fields you search or sort frequently. Avoid indexing highly updated fields.
- Open
.mongodb.jsfile in VS Code. - Click Run Playground.
- Or use
mongoshmanually to run commands. - Visualize results using MongoDB Compass.