Skip to content
BackendDatabaseExpressJsMongoDBNodeJs

1. MongoDB Setup – CRUD Operations समझे (Beginner Guide) 2026

April 26, 2026 12 min read

नमस्ते दोस्तों! 🙏
स्वागत है The Easy Master पर!

क्या तुमने कभी सोचा है – Data कहाँ store होता है? जब तुम Instagram पर post करते हो, product order करते हो – सब data database में जाता है।

MongoDB ek NoSQL database है – data JSON जैसे documents में store होता है। SQL databases से ज्यादा flexible और easy है।

MongoDB CRUD operations Hindi में समझना बहुत जरूरी है क्योंकि:

  • MERN stack MongoDB par based है
  • Flexible schema – fields बदल सकते हो
  • JSON-like documents – JavaScript developers के लिए easy
  • Scalable – बड़े apps के लिए perfect
  • Interview mein pakka MongoDB questions puche jayenge

Aaj kya seekhoge?

TopicKya Seekhega?
MongoDB Kya Hai?NoSQL database introduction
InstallationLocal ya Atlas setup
MongoDB ShellCommand line interface
MongoDB CompassGUI tool
Create (Insert)Documents insert karna
Read (Find)Documents search karna
UpdateDocuments modify karna
DeleteDocuments remove karna

Kya tumhe pata hai?
MongoDB ka naam “humongous” से आया है – बहुत बड़े data handle करने के लिए!

तो चलिए शुरू करते हैं – MongoDB CRUD operations Hindi सीखने का सफर! 🚀

Table of Contents

1. MongoDB क्या Hai? – Introduction

MongoDB ek NoSQL document database है जो data को JSON-like documents में store करता है .

MongoDB vs Traditional Database:

Traditional (SQL)MongoDB (NoSQL)
TablesCollections
RowsDocuments
ColumnsFields
Fixed SchemaFlexible Schema
JOINsEmbedded Documents

MongoDB Document Example:

Code
{
"_id": ObjectId("65f1a2b3c4d5e6f7g8h9i0j1"),
"name": "Vivek Sharma",
"email": "vivek@example.com",
"age": 25,
"address": {
"city": "Mumbai",
"pincode": 400001
},
"hobbies": ["coding", "reading", "gaming"],
"createdAt": ISODate("2026-01-15T10:30:00Z")
}

MongoDB CRUD operations Hindi में हम shell और Compass दोनों सीखेंगे।

2. SQL vs NoSQL – Comparison

SQL Databases (MySQL, PostgreSQL):

Code
┌─────────────────────────────────────┐
│ USERS TABLE │
├─────┬───────────┬──────────────────┤
│ id │ name │ email │
├─────┼───────────┼──────────────────┤
│ 1 │ Vivek │ vivek@test.com │
│ 2 │ Deepti │ deepti@test.com │
└─────┴───────────┴──────────────────┘

- Fixed schema (columns fixed)
- Data related across tables (JOINs)
- ACID transactions

NoSQL Databases (MongoDB):

Code
// Collection: users
[
{ "_id": 1, "name": "Vivek", "email": "vivek@test.com" },
{ "_id": 2, "name": "Deepti", "email": "deepti@test.com", "phone": "1234567890" }
]

- Flexible schema (fields can vary)
- Embedded documents (no JOINs needed)
- High scalability

When to Use MongoDB:

Use MongoDBUse SQL
Rapid developmentComplex transactions
Flexible schemaFixed schema
Large scale dataData integrity critical
JSON/JavaScript appsReporting & analytics
Real-time analyticsMulti-row transactions

3. Installation – MongoDB Setup

  1. Sign up at mongodb.com/atlas
  2. Create free cluster
  3. Get connection string
  4. No installation needed!

Option 2: Local Installation (Ubuntu)

Code
# Import MongoDB public key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -

# Add MongoDB repository
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

# Update and install
sudo apt update
sudo apt install -y mongodb-org

# Start MongoDB service
sudo systemctl start mongod
sudo systemctl enable mongod

# Check status
sudo systemctl status mongod

# Start MongoDB shell
mongosh

Source: 

Option 3: Local Installation (Windows/Mac)

4. MongoDB Compass – GUI Tool

MongoDB Compass GUI tool है – बिना code likhe database manage karne के लिए .

Installation:

  1. Download from mongodb.com/products/compass
  2. Install for your OS
  3. Open Compass

Connect to Database:

Code
Connection String: mongodb://localhost:27017
Or Atlas: mongodb+srv://username:password@cluster.mongodb.net/

Compass Features:

FeatureUse
Database ExplorerView all databases
Collection ViewerSee documents
Visual Query BuilderBuild queries without code
Schema AnalyzerUnderstand data structure
Index ManagerCreate/manage indexes
Aggregation BuilderBuild pipelines visually

Using Compass:

  1. Create Database: Click “+” → Enter name → Create
  2. Create Collection: Click “Create Collection” → Enter name
  3. Insert Document: Click “Add Data” → Insert Document
  4. Query Data: Enter filter like { "name": "Vivek" }
  5. Update Document: Click edit icon → Modify → Update

5. MongoDB Shell – Command Line

MongoDB Shell (mongosh) command line interface है .

Start MongoDB Shell:

Code
# Start shell
mongosh

# Connect to specific database
mongosh mongodb://localhost:27017/mydb

# With Atlas
mongosh "mongodb+srv://cluster.mongodb.net/mydb" --username myuser

Basic Shell Commands:

Code
// Show all databases
show dbs

// Switch to/create database
use mydb

// Show current database
db

// Show all collections
show collections

// Get database stats
db.stats()

// Drop current database
db.dropDatabase()

Help Commands:

Code
// General help
help

// Database methods help
db.help()

// Collection methods help
db.users.help()

6. Databases aur Collections

Database Commands:

Code
// List all databases
show dbs

// Create/switch to database
use shopDB

// Check current database
db

// Get database stats
db.stats()
// Output: { "db": "shopDB", "collections": 0, "objects": 0, ... }

// Drop database (WARNING: deletes everything!)
db.dropDatabase()

Collection Commands:

Code
// Create collection explicitly
db.createCollection("products")

// Create collection by inserting (auto-created)
db.users.insertOne({ name: "Vivek" })

// List collections
show collections
// or
db.getCollectionNames()

// Get collection stats
db.products.stats()

// Drop collection
db.products.drop()

7. CREATE – Insert Operations

insertOne() – Single Document:

Code
// Insert one document
db.users.insertOne({
name: "Vivek Sharma",
email: "vivek@example.com",
age: 25,
city: "Mumbai"
})

// Output:
{
acknowledged: true,
insertedId: ObjectId("65f1a2b3c4d5e6f7g8h9i0j1")
}

insertMany() – Multiple Documents:

Code
// Insert multiple documents
db.users.insertMany([
  {
    name: "Priya Patel",
    email: "priya@example.com",
    age: 23,
    city: "Delhi"
  },
  {
    name: "Amit Kumar",
    email: "amit@example.com",
    age: 28,
    city: "Bangalore"
  },
  {
    name: "Neha Gupta",
    email: "neha@example.com",
    age: 26,
    city: "Mumbai"
  }
])

// Output:
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId("..."),
    '1': ObjectId("..."),
    '2': ObjectId("...")
  }
}

Insert with Different Field Types:

Code
db.products.insertOne({
  name: "MacBook Pro",
  price: 150000,
  inStock: true,
  tags: ["electronics", "apple", "laptop"],
  specifications: {
    ram: "16GB",
    storage: "512GB",
    processor: "M3"
  },
  createdAt: new Date()
})

8. READ – Find Operations

find() – Find Multiple Documents:

Code
// Find all documents
db.users.find()

// Format output (pretty print)
db.users.find().pretty()

// Find with filter
db.users.find({ city: "Mumbai" })

// Find with multiple conditions
db.users.find({ city: "Mumbai", age: { $gt: 20 } })

findOne() – Find Single Document:

Code
// Find first matching document
db.users.findOne({ name: "Vivek Sharma" })

// Find by _id
db.users.findOne({ _id: ObjectId("65f1a2b3c4d5e6f7g8h9i0j1") })

Query Operators:

Code
// Comparison operators
db.users.find({ age: { $gt: 25 } })   // Greater than
db.users.find({ age: { $gte: 25 } })  // Greater than or equal
db.users.find({ age: { $lt: 25 } })   // Less than
db.users.find({ age: { $lte: 25 } })  // Less than or equal
db.users.find({ age: { $ne: 25 } })   // Not equal

// Logical operators
db.users.find({
  $or: [
    { city: "Mumbai" },
    { city: "Delhi" }
  ]
})

db.users.find({
  $and: [
    { age: { $gt: 20 } },
    { age: { $lt: 30 } }
  ]
})

// Array operators
db.users.find({ hobbies: { $in: ["coding", "reading"] } })
db.users.find({ tags: { $all: ["electronics", "laptop"] } })

Projection (Select Specific Fields):

Code
// Include only name and email (exclude _id)
db.users.find(
{ city: "Mumbai" },
{ name: 1, email: 1, _id: 0 }
)

// Exclude password
db.users.find(
{ email: "vivek@example.com" },
{ password: 0 }
)

Sorting, Limiting, Skipping:

Code
// Sort by age ascending
db.users.find().sort({ age: 1 })

// Sort by age descending
db.users.find().sort({ age: -1 })

// Limit results
db.users.find().limit(5)

// Skip results (pagination)
db.users.find().skip(10).limit(5)

// Chain operations
db.users.find({ city: "Mumbai" })
  .sort({ age: -1 })
  .skip(0)
  .limit(10)

9. UPDATE – Update Operations

updateOne() – Update Single Document:

Code
// Update one document
db.users.updateOne(
{ name: "Vivek Sharma" }, // Filter
{ $set: { age: 26, city: "Pune" } } // Update
)

// Output:
{ acknowledged: true, matchedCount: 1, modifiedCount: 1 }

updateMany() – Update Multiple Documents:

Code
// Update all users in Mumbai
db.users.updateMany(
  { city: "Mumbai" },
  { $set: { state: "Maharashtra" } }
)

// Increment age by 1 for all users
db.users.updateMany(
  {},
  { $inc: { age: 1 } }
)

Update Operators:

OperatorUseExample
$setSet field value{ $set: { name: "New" } }
$unsetRemove field{ $unset: { temp: "" } }
$incIncrement number{ $inc: { age: 1 } }
$mulMultiply number{ $mul: { price: 1.1 } }
$renameRename field{ $rename: { "old": "new" } }
$minSet if less than current{ $min: { age: 18 } }
$maxSet if greater than current{ $max: { score: 100 } }

Array Update Operators:

Code
// Add element to array
db.users.updateOne(
{ name: "Vivek" },
{ $push: { hobbies: "swimming" } }
)

// Add multiple elements
db.users.updateOne(
{ name: "Vivek" },
{ $push: { hobbies: { $each: ["cycling", "traveling"] } } }
)

// Remove element from array
db.users.updateOne(
{ name: "Vivek" },
{ $pull: { hobbies: "gaming" } }
)

// Add to array if not exists
db.users.updateOne(
{ name: "Vivek" },
{ $addToSet: { hobbies: "reading" } }
)

findOneAndUpdate():

Code
// Update and return updated document
db.users.findOneAndUpdate(
{ name: "Vivek Sharma" },
{ $set: { age: 27 } },
{ returnNewDocument: true }
)

// Update and return original document
db.users.findOneAndUpdate(
{ name: "Vivek Sharma" },
{ $set: { age: 27 } }
)

replaceOne() – Full Document Replacement:

Code
// Replace entire document (except _id)
db.users.replaceOne(
{ name: "Vivek Sharma" },
{
name: "Vivek Kumar",
email: "vivek@example.com",
age: 28,
city: "Chennai"
}
)

10. DELETE – Delete Operations

deleteOne() – Delete Single Document:

Code
// Delete first matching document
db.users.deleteOne({ name: "Amit Kumar" })

// Output:
{ acknowledged: true, deletedCount: 1 }

// Delete by _id
db.users.deleteOne({ _id: ObjectId("65f1a2b3c4d5e6f7g8h9i0j1") })

deleteMany() – Delete Multiple Documents:

Code
// Delete all users from Delhi
db.users.deleteMany({ city: "Delhi" })

// Delete users older than 30
db.users.deleteMany({ age: { $gt: 30 } })

// Delete all documents (be careful!)
db.users.deleteMany({})

findOneAndDelete():

Code
// Delete and return deleted document
const deletedUser = db.users.findOneAndDelete(
{ name: "Vivek Sharma" }
)

// Output: Returns the deleted document

11. Query Operators – $gt, $lt, $or, $in

Complete Operators Reference:

Code
// Comparison Operators
$eq  - Equal to          { age: { $eq: 25 } }
$ne  - Not equal to      { age: { $ne: 25 } }
$gt  - Greater than      { age: { $gt: 25 } }
$gte - Greater or equal  { age: { $gte: 25 } }
$lt  - Less than         { age: { $lt: 25 } }
$lte - Less or equal     { age: { $lte: 25 } }
$in  - In array          { city: { $in: ["Mumbai", "Delhi"] } }
$nin - Not in array      { city: { $nin: ["Mumbai"] } }

// Logical Operators
$or  - OR condition      { $or: [ { age: 25 }, { city: "Mumbai" } ] }
$and - AND condition     { $and: [ { age: 25 }, { city: "Mumbai" } ] }
$not - NOT condition     { age: { $not: { $gt: 25 } } }
$nor - NOR condition     { $nor: [ { age: 25 }, { city: "Mumbai" } ] }

// Element Operators
$exists - Field exists   { email: { $exists: true } }
$type   - Field type     { age: { $type: "number" } }

// Array Operators
$all   - All elements    { tags: { $all: ["mongodb", "express"] } }
$size  - Array size      { hobbies: { $size: 3 } }

Practical Examples:

Code
// Find users between age 20 and 30
db.users.find({ age: { $gt: 20, $lt: 30 } })

// Find users from Mumbai or Delhi
db.users.find({ city: { $in: ["Mumbai", "Delhi"] } })

// Find users with age > 25 OR city Mumbai
db.users.find({
  $or: [
    { age: { $gt: 25 } },
    { city: "Mumbai" }
  ]
})

// Find users with email field exists
db.users.find({ email: { $exists: true } })

// Find users with at least 3 hobbies
db.users.find({ hobbies: { $size: 3 } })

12. Quick Cheat Sheet

Database Commands:

CommandPurpose
show dbsList databases
use dbnameSwitch/create database
dbShow current database
db.dropDatabase()Delete database

Collection Commands:

CommandPurpose
show collectionsList collections
db.createCollection("name")Create collection
db.collection.drop()Delete collection

CRUD Commands:

OperationCommand
Createdb.collection.insertOne(doc)
Create Manydb.collection.insertMany([docs])
Read Alldb.collection.find()
Read Onedb.collection.findOne(filter)
Update Onedb.collection.updateOne(filter, update)
Update Manydb.collection.updateMany(filter, update)
Delete Onedb.collection.deleteOne(filter)
Delete Manydb.collection.deleteMany(filter)

Query Operators:

OperatorMeaning
$gtGreater than
$ltLess than
$gteGreater or equal
$lteLess or equal
$eqEqual
$neNot equal
$inIn array
$orOR condition
$andAND condition
$setSet value (update)
$incIncrement (update)
$pushAdd to array
$pullRemove from array

13. FAQ

Q1: MongoDB CRUD operations Hindi में सबसे important kya hai?
insertOne(), find(), updateOne(), deleteOne() – ye charo basic CRUD operations हैं।

Q2: MongoDB vs MySQL – kya use karein?
Flexible schema चाहिए, rapid development करना है – MongoDB। Complex transactions, data integrity चाहिए – MySQL।

Q3: MongoDB Atlas kya hai?
Cloud-based MongoDB service – free tier available, no installation needed।

Q4: find() vs findOne() – kya antar hai?
find() returns cursor (multiple documents), findOne() returns single document.

Q5: updateOne() vs updateMany() – kya antar hai?
updateOne() – sirf first matching document update, updateMany() – सभी matching documents update।

Q6: $set operator kya karta hai?
Document में field set करता है – agar field exists नहीं तो create करता है।

Q7: $inc operator kya karta hai?
Numeric field की value increment/decrement करता है।

Q8: MongoDB Compass kya hai?
GUI tool hai – बिना code likhe database manage करने के लिए।

Q9: _id field kya hai?
Unique identifier for each document – automatically generated (ObjectId)।

Q10: Index kyun banayein?
Query performance improve करने के लिए – frequently queried fields पर index banाएं।

14. Conclusion

बहुत बढ़िया दोस्तों! आज हमने MongoDB CRUD operations Hindi को पूरी detail में समझा।

Quick Recap:

OperationMethodExample
CreateinsertOne() / insertMany()db.users.insertOne({name:"Vivek"})
Readfind() / findOne()db.users.find({city:"Mumbai"})
UpdateupdateOne() / updateMany()db.users.updateOne(filter, {$set:{age:26}})
DeletedeleteOne() / deleteMany()db.users.deleteOne({name:"Vivek"})

Mera personal experience:

MongoDB seekhne ke baad maine bohot saare projects banaye – flexible schema की वजह से development bohot fast हो गया। Compass GUI से data देखना और debug करना भी easy है।

Tum bhi ye steps follow karo:

  1. ✅ MongoDB Atlas account banao
  2. ✅ Compass install करो
  3. ✅ Pehla database aur collection banao
  4. ✅ insertOne() और find() try करो
  5. ✅ updateOne() और deleteOne() try करो

अब तुम्हारी बारी है!

नीचे comment में बताओ:

  1. तुम MongoDB use karoge ya SQL?
  2. कौन सा CRUD operation सबसे easy लगा?
  3. अगला topic क्या चाहिए? (MongoDB with Node.js? Mongoose ODM? Aggregation Pipeline?)

The Easy Master पर बने रहो। Happy Coding! 🚀🍃

Resource

Additional Resources

TheEasyMaster

Author at The Easy Master.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *