SRCarlo/MongoDB-Magic
A hands-on MongoDB practice repository focused on real-world database operations, including CRUD workflows, query optimization, aggregation pipelines, indexing, and core administrative tasks. Structured into organized scripts, it supports progressive learning and reinforces practical NoSQL database design principles.
MongoDB Operations: A Comprehensive Guide
A collection of MongoDB scripts to demonstrate various operations with MongoDB. This repository covers setup, reading, updating, deleting, aggregation, indexing, and administrative commands.
Table of Contents
- Setup and Insert Data
- Reading Data
- Updating Documents
- Deleting Documents
- Aggregation Examples
- Indexing and Performance
- Admin Commands
- Contact Information
- License
- Folder Structure
Setup and Insert Data
- Initialize the database and insert sample data.
- Script:
01_setup.mongodb.js
Example:
// Connect to MongoDB
const { MongoClient } = require("mongodb");
const url = "mongodb://localhost:27017";
const client = new MongoClient(url);
async function run() {
try {
await client.connect();
console.log("Connected to MongoDB!");
const db = client.db("ecommerce");
const products = db.collection("products");
// More setup code...
} finally {
await client.close();
}
}
run().catch(console.error);Reading Data
- Find all documents and pretty print them.
- Filter data using queries.
- Script:
02_reading.mongodb.js
Example:
db.products.find();
db.products.find().pretty();Updating Documents
- Update single and multiple documents.
- Script:
03_update.mongodb.js
Example:
db.products.updateOne({ name: "Wireless Mouse" }, { $set: { price: 899 } });Deleting Documents
- Delete individual and multiple documents.
- Script:
04_delete.mongodb.js
Example:
db.contacts.deleteOne({ name: "Alice" });Aggregation Examples
- Perform aggregation operations like
$match,$group, and$sort. - Script:
05_aggregation.mongodb.js
Example:
db.orders.aggregate([{ $group: { _id: "$status", totalOrders: { $sum: 1 } } }]);Indexing and Performance
- Create and manage indexes for better performance.
- Script:
06_indexes.mongodb.js
Example:
db.products.createIndex({ name: 1 });Admin Commands
- Useful commands for managing the database.
- Script:
07_AdminCommands.mogodb.js
Example:
db.stats();
db.serverStatus();Contact Information
For any inquiries, suggestions, or feedback, please contact the repository maintainer:
- Email: asphaltshubhuu@gmail.com
- LinkedIn: Shubham Raut
- GitHub: SRCarlo
Authors
- Shubham Raut – Initial work – SRCarlo – MongoDB-Magic
Clone the Repository
To clone the repository, run the following command:
git clone https://github.com/SRCarlo/MongoDB-Magic.gitFolder Structure
Here’s the structure of the repository:
MONGODB/
│
├── 01_setup.mongodb.js # Database setup and insertion scripts
├── 02_reading.mongodb.js # Reading data from MongoDB
├── 03_update.mongodb.js # Update documents in MongoDB
├── 04_delete.mongodb.js # Deleting documents
├── 05_aggregation.mongodb.js # Aggregation operations
├── 06_indexes.mongodb.js # Indexing and performance
├── 07_AdminCommands.mongodb.js # Useful admin commands
├── MongoDB Handbook.pdf # MongoDB HandBook
└── README.md # This README file
License
This repository is licensed under the MIT License. ## License
MIT License
Copyright (c) 2026 SRCarlo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit others to whom the Software is provided to do so,
subject to the following conditions:
-
Copyright Notice: The above copyright notice and this permission notice must be included in all copies or substantial portions of the Software.
-
Modification Disclaimer: If you modify, distribute, or otherwise use this Software, you must ensure that you do not claim the original authorship unless otherwise authorized in writing.
Disclaimer of Warranty and Liability:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.