A collection of MongoDB scripts to demonstrate various operations with MongoDB. This repository covers setup, reading, updating, deleting, aggregation, indexing, and administrative commands.
- Setup and Insert Data
- Reading Data
- Updating Documents
- Deleting Documents
- Aggregation Examples
- Indexing and Performance
- Admin Commands
- Contact Information
- License
- Folder Structure
- Initialize the database and insert sample data.
- Script:
01_setup.mongodb.js
// 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);- Find all documents and pretty print them.
- Filter data using queries.
- Script:
02_reading.mongodb.js
db.products.find();
db.products.find().pretty();- Update single and multiple documents.
- Script:
03_update.mongodb.js
db.products.updateOne({ name: "Wireless Mouse" }, { $set: { price: 899 } });- Delete individual and multiple documents.
- Script:
04_delete.mongodb.js
db.contacts.deleteOne({ name: "Alice" });- Perform aggregation operations like
$match,$group, and$sort. - Script:
05_aggregation.mongodb.js
db.orders.aggregate([{ $group: { _id: "$status", totalOrders: { $sum: 1 } } }]);- Create and manage indexes for better performance.
- Script:
06_indexes.mongodb.js
db.products.createIndex({ name: 1 });- Useful commands for managing the database.
- Script:
07_AdminCommands.mogodb.js
db.stats();
db.serverStatus();For any inquiries, suggestions, or feedback, please contact the repository maintainer:
- Email: asphaltshubhuu@gmail.com
- LinkedIn: Shubham Raut
- GitHub: SRCarlo
- Shubham Raut – Initial work – SRCarlo – MongoDB-Magic
To clone the repository, run the following command:
git clone https://github.com/SRCarlo/MongoDB-Magic.gitHere’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
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.
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.