MongoDB Crash Course | Complete Guide
MongoDB is a NoSQL database widely used in modern applications. It’s fast, flexible, and schema-less, which means you don’t need fixed tables like in SQL databases. Data is stored in documents (JSON-like objects) inside collections (similar to tables), which are part of databases.

I. MongoDB Installation & Setup
Install MongoDB – Use the Community Edition from MongoDB’s official site.
Open Mongo Shell (
mongo sh) – Run queries interactively.
mongo sh
Command  | Explanation  | 
|---|---|
  | List all databases  | 
  | Switch to or create a new database  | 
  | List all collections in current database  | 
  | Delete the current database  | 
  | Exit Mongo shell  | 
Think of databases as big cabinets, collections as folders, and documents as profile sheets inside folders.
II. Core Concepts
Document – JSON-like object:
{name: "Aarav", age: 25}Collection – Group of documents, like a table called
usersDatabase – Contains collections, like
schoolDBSchema-less – Each document can have different fields
III. Creating Data (C in CRUD)
MongoDB allows creating documents using insertOne and insertMany.
// Insert single user
db.users.insertOne({name: "Aarav"})
// Insert user with multiple fields
db.users.insertOne({
  name: "Priya",
  age: 22,
  address: {street: "MG Road", city: "Bangalore"},
  hobbies: ["dancing", "painting"]
})
// Insert multiple users at once
db.users.insertMany([
  {name: "Rohit", age: 30},
  {name: "Ananya", age: 28},
  {name: "Karan", age: 24},
  {name: "Sneha", age: 26}
])
Each document is like a profile sheet, and collections group these sheets together.
IV. Reading Data (R in CRUD)
1. Basic Queries
db.users.find() // Get all users
db.users.find().limit(2) // Only first 2 users
db.users.find().skip(1).limit(2) // Skip 1, get next 2
db.users.find().sort({name: 1}) // Sort A-Z
db.users.find().sort({name: -1}) // Sort Z-A
db.users.findOne({age: 22}) // Get first match with age 22
db.users.countDocuments({age: {$lte: 30}}) // Count users age ≤ 30
find() shows all the documents that match a given condition.
2. Filtering Queries
Operator  | Explanation  | Example  | 
|---|---|---|
  | Equal  | 
  | 
  | Not equal  | 
  | 
  | Greater than / ≥  | 
  | 
  | Less than / ≤  | 
  | 
  | In / Not in array  | 
  | 
  | Field exists  | 
  | 
  | OR condition  | 
  | 
  | AND condition  | 
  | 
  | Negates  | 
  | 
  | Compare fields  | 
  | 
Example:
db.users.find({$or: [{age: {$lte: 25}}, {name: "Rohit"}]})
These operators help filter the documents to match the conditions you want.
3. Projection (Selecting Specific Fields)
db.users.find({name: "Priya"}, {name:1, age:1, _id:0}) // Show only name & age
db.users.find({name: "Priya"}, {age:0}) // Show everything except age
db.users.find({}, {name:1, hobbies:1}) // Show name and hobbies for all users
Projection lets you select only the fields you want to see.
V. Updating Data (U in CRUD)
// Update one user
db.users.updateOne({name: "Aarav"}, {$set: {age: 26}})
// Update multiple users
db.users.updateMany({age: {$gte: 25}}, {$inc: {age: 1}}) // Add 1 year
// Rename field
db.users.updateOne({name: "Priya"}, {$rename: {name: "firstName"}})
// Remove a field
db.users.updateOne({name: "Rohit"}, {$unset: {hobbies: ""}})
// Add to array
db.users.updateOne({name: "Ananya"}, {$push: {hobbies: "singing"}})
// Remove from array
db.users.updateOne({name: "Ananya"}, {$pull: {hobbies: "singing"}})
// Replace whole document
db.users.replaceOne({name: "Karan"}, {name: "Karan", age: 25, city: "Delhi"})
Updating is like editing a profile sheet to change, add, or remove details.
VI. Deleting Data (D in CRUD)
// Delete one user
db.users.deleteOne({name: "Rohit"})
// Delete multiple users
db.users.deleteMany({age: {$exists: false}})
Deleting removes unwanted documents from the collection.
VII. Aggregation (Advanced Queries)
// Count users per age
db.users.aggregate([
  {$group: {_id: "$age", total: {$sum: 1}}}
])
// Average age
db.users.aggregate([
  {$group: {_id: null, avgAge: {$avg: "$age"}}}
])
Aggregation is used to summarize or analyze data across multiple documents.
VIII. Summary
MongoDB is:
Flexible & fast – Great for apps with dynamic data
Schema-less – Easy to start without defining tables
JavaScript-friendly – Commands use JSON-style objects
Powerful – Supports CRUD operations, filtering, projections, updates, deletions, and aggregation
Introduction to Node.js (Full Interview Guide)
Explore Node.js with our concise infographic detailing its core functionalities. Learn how this JavaScript runtime powers fast, scalable backend development, real-time apps, and robust APIs. Perfect for developers and businesses looking to understand Node.js benefits.
Read Full Story📝 Introduction to NestJS with CRUD Example
Beginner-friendly NestJS tutorial with CRUD example. Learn setup, folder structure, and simple GET, POST, PUT, DELETE API routes in NestJS.
Read Full StoryNest.js Vs Express.js |Comparison
Compare NestJS vs ExpressJS with simple CRUD examples. Learn which Node.js framework is best for quick apps or scalable backend development
Read Full Story🛣️ Node.js Roadmap
Explore a comprehensive Node.js roadmap. Discover key stages for learning and mastering Node.js development, from foundational concepts to advanced topics.
Read Full Story