Practical MongoDB codes, real-world queries, and data-analytics focused learning.
This repo is my MongoDB practice space where I learn and teach myself:
- How to use MongoDB as a NoSQL database for projects.
- How to write clean, optimised queries for analytics & reporting.
- How to design better data models for real-world applications.
All examples are written in a way that students, beginners, and data analysts can easily follow.
You can create folders like this in your repo:
MongoDB/
βββ 01_Basics/
β βββ commands.md
β βββ intro_notes.md
βββ 02_CRUD/
β βββ insert_examples.md
β βββ find_queries.md
β βββ update_examples.md
β βββ delete_examples.md
βββ 03_Query_Operators/
βββ 04_Aggregation/
βββ 05_Data_Modeling/
βββ 06_Indexing_Performance/
βββ 07_NodeJS_Integration/
βββ README.md
- What is MongoDB, BSON, Collections, Documents
- Installing & connecting with
mongosh - Basic commands:
show dbs,use,show collections,db.dropDatabase()
Sample commands:
// check DB
show dbs;
use my_learning_db;
db;- Insert documents with
insertOne/insertMany - Read data using
find,findOne, projections - Update using
$set,$inc,$push,$addToSet - Delete using
deleteOne,deleteMany
Example:
// Create
db.students.insertOne({ name: "Ashwin", course: "MongoDB", active: true });
// Read
db.students.find({ active: true }, { _id: 0, name: 1, course: 1 });
// Update
db.students.updateOne(
{ name: "Ashwin" },
{ $set: { active: false }, $inc: { attempts: 1 } }
);
// Delete
db.students.deleteOne({ name: "Ashwin" });- Comparison:
$gt,$gte,$lt,$lte,$eq,$ne - Logical:
$and,$or,$not,$nor - Array & element:
$in,$nin,$all,$exists,$size
Example:
// students with marks between 60 and 90
db.students.find({
marks: { $gte: 60, $lte: 90 }
});
// active students in selected courses
db.students.find({
active: true,
course: { $in: ["MongoDB", "Node.js", "Data Analytics"] }
});-
sort()β ascending / descending -
limit()β top N records -
skip()β for pagination
Example:
// top 5 highest scorers
db.students.find().sort({ marks: -1 }).limit(5);
// simple pagination
db.students.find().skip(10).limit(10);-
$matchβ filter -
$groupβ aggregate (sum, avg, count, min, max) -
$projectβ reshape fields -
$sort,$limit,$lookup,$unwind
Example: Average marks per course
db.students.aggregate([
{ $match: { active: true } },
{
$group: {
_id: "$course",
avgMarks: { $avg: "$marks" },
totalStudents: { $sum: 1 }
}
},
{ $sort: { avgMarks: -1 } }
]);-
Designing embedded vs referenced structures
-
Handling one-to-many / many-to-many relationships
-
Structuring collections for:
- StudentβCourseβResult
- TasksβProjects
- OrdersβCustomers
Sample embedded model:
db.courses.insertOne({
title: "MongoDB Basics",
level: "Beginner",
instructor: {
name: "Ashwin",
experienceYears: 3
}
});- Creating single-field and compound indexes
- Analyzing queries with
explain() - Understanding when NOT to overuse indexes
Example:
// index on email
db.students.createIndex({ email: 1 }, { unique: true });
// check indexes
db.students.getIndexes();Folder: 07_NodeJS_Integration/
- Connecting using MongoDB Node.js driver
- Basic CRUD from Node.js
- Environment variables for secure connection strings
Mini example:
const { MongoClient } = require("mongodb");
const client = new MongoClient("mongodb://localhost:27017");
async function run() {
await client.connect();
const db = client.db("my_learning_db");
const students = db.collection("students");
const data = await students.find({ active: true }).toArray();
console.log(data);
await client.close();
}
run();- MongoDB fundamentals (DB, collection, document)
- CRUD operations & query operators
- Aggregation for analytics
- Data modeling & schema design thinking
- Indexing and basic performance tuning
- Backend + database thinking (Node.js integration)
GitHub topics:
mongodb, nosql, database, crud, aggregation-framework,
data-analytics, data-modeling, nodejs, learning-notes, tutorials
Social hashtags:
#MongoDB #NoSQL #DataAnalytics #BackendDeveloper
#DatabaseDesign #LearningInPublic #AshwinPanbude
