तो स्वागत है आपका। 👋
अगर आप Database सीख रहे हैं, तो आपने MongoDB का नाम जरूर सुना होगा। MongoDB एक popular NoSQL Database है, जिसमें Data को Tables की जगह Collections और Documents के रूप में रखा जाता है।
शुरुआत में MongoDB थोड़ा अलग लग सकता है, खासकर अगर आपने पहले MySQL या किसी दूसरे SQL Database पर काम किया है। लेकिन इसके Basic Commands और Queries समझ आने के बाद MongoDB काफी आसान लगने लगता है।
इस Complete Cheat Sheet में हम MongoDB के जरूरी Database Commands, CRUD Queries, Operators, Sorting, Searching, Indexes और Aggregation को आसान भाषा में समझेंगे।
💡 मेरा अनुभव
MongoDB सीखते समय सबसे ज्यादा Confusion आमतौर पर SQL और MongoDB के बीच होता है। SQL में हम Tables और Rows के साथ काम करते हैं, जबकि MongoDB में Collections और Documents होते हैं।
लेकिन जब insertOne(), find(), updateOne() और deleteOne() जैसे Commands की Practice शुरू करते हैं, तो MongoDB का काम काफी आसान लगने लगता है।
मेरी सलाह है कि हर Command को पढ़ने के साथ-साथ खुद भी चलाकर देखें।
MongoDB क्या है?
MongoDB एक NoSQL Document Database है।
इसमें Data JSON जैसे दिखने वाले BSON Documents में Store होता है।
एक MongoDB Document कुछ ऐसा दिख सकता है:
{
name: "Rahul",
age: 22,
city: "Delhi"
}
यहाँ:
nameएक Field है।ageएक Field है।cityएक Field है।पूरा Object एक Document है।
MongoDB में Database Structure
MongoDB को इस तरह समझ सकते हैं:
MongoDB
↓
Database
↓
Collection
↓
Document
↓
Field
अगर SQL Database से तुलना करें:
| SQL | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
| SQL Query | MongoDB Query |
MongoDB क्यों इस्तेमाल किया जाता है?
MongoDB की कुछ खास बातें हैं:
Flexible Data Structure
JSON जैसी आसान Structure
बड़े Data के साथ काम कर सकता है
JavaScript और Node.js के साथ आसानी से इस्तेमाल होता है
जल्दी Development किया जा सकता है
कई तरह के Applications में इस्तेमाल होता है
MongoDB कहाँ इस्तेमाल होता है?
MongoDB का इस्तेमाल कई तरह के Projects में किया जा सकता है:
Websites
Mobile Apps
REST APIs
E-commerce Projects
Blog Websites
Social Media Apps
Chat Applications
Content Management Systems
MongoDB कैसे Install करें?
MongoDB इस्तेमाल करने के लिए आप MongoDB Community Edition Install कर सकते हैं या MongoDB Atlas जैसी Cloud Service का इस्तेमाल कर सकते हैं।
MongoDB Install होने के बाद आप MongoDB Shell (mongosh) से Database के साथ काम कर सकते हैं।
Version Check:
mongosh --version
MongoDB Shell शुरू करें
mongosh
अब आप MongoDB Commands चला सकते हैं।
Database की List देखें
show dbs
यह आपके MongoDB Server पर मौजूद Databases दिखाएगा।
Database चुनें
use school
अगर school नाम का Database पहले से नहीं है, तो MongoDB में Data Store करने के बाद यह Database दिखाई देगा।
Current Database देखें
db
Output में Current Database का नाम दिखाई देगा।
Database बनाएँ
MongoDB में Database को अलग से बनाने के बजाय उसमें Data Store करने पर Database तैयार होता है।
use school
इसके बाद Collection में कोई Document Insert करें।
Collection देखें
show collections
Collection बनाएँ
db.createCollection("students")
अब students नाम की Collection तैयार हो जाएगी।
Collection हटाएँ
db.students.drop()
⚠️ इससे Collection और उसका Data Delete हो सकता है।
Database हटाएँ
पहले सही Database चुनें:
use school
फिर:
db.dropDatabase()
इस Command का इस्तेमाल बहुत ध्यान से करें।
CRUD क्या है?
MongoDB में Data के साथ चार मुख्य काम होते हैं:
C → Create
R → Read
U → Update
D → Delete
इन्हें मिलाकर CRUD कहा जाता है।
Document Insert करें
एक Document Insert करें
db.students.insertOne({
name: "Rahul",
age: 20,
city: "Delhi"
})
कई Documents Insert करें
db.students.insertMany([
{
name: "Aman",
age: 21,
city: "Mumbai"
},
{
name: "Ravi",
age: 22,
city: "Amritsar"
}
])
सभी Documents देखें
db.students.find()
Documents को थोड़ा साफ़ तरीके से देखने के लिए:
db.students.find().pretty()
एक Document देखें
db.students.findOne()
किसी नाम से Search करें
db.students.find({
name: "Rahul"
})
Age से Search करें
db.students.find({
age: 20
})
एक से ज्यादा Conditions
db.students.find({
city: "Delhi",
age: 20
})
यहाँ दोनों Conditions को पूरा करने वाले Documents मिलेंगे।
Comparison Operators
MongoDB में Data को Compare करने के लिए कई Operators होते हैं।
| Operator | मतलब |
|---|---|
$eq | बराबर |
$ne | बराबर नहीं |
$gt | से बड़ा |
$gte | बड़ा या बराबर |
$lt | से छोटा |
$lte | छोटा या बराबर |
$in | दिए गए Values में |
$nin | दिए गए Values में नहीं |
$gt Operator
20 से ज्यादा Age वाले Students:
db.students.find({
age: { $gt: 20 }
})
$gte Operator
20 या उससे ज्यादा:
db.students.find({
age: { $gte: 20 }
})
$lt Operator
25 से कम:
db.students.find({
age: { $lt: 25 }
})
$lte Operator
25 या उससे कम:
db.students.find({
age: { $lte: 25 }
})
$ne Operator
db.students.find({
city: { $ne: "Delhi" }
})
यह Delhi के अलावा दूसरे Cities के Documents खोजेगा।
$in Operator
db.students.find({
city: {
$in: ["Delhi", "Mumbai"]
}
})
$nin Operator
db.students.find({
city: {
$nin: ["Delhi", "Mumbai"]
}
})
Logical Operators
मुख्य Logical Operators:
$and$or$not$nor
$or
db.students.find({
$or: [
{ city: "Delhi" },
{ age: 20 }
]
})
$and
db.students.find({
$and: [
{ age: { $gte: 18 } },
{ city: "Delhi" }
]
})
$not
db.students.find({
age: {
$not: { $gt: 20 }
}
})
Array Operators
MongoDB में Arrays के लिए भी कई Operators मिलते हैं।
$all$elemMatch$size
उदाहरण:
db.students.find({
skills: {
$all: ["HTML", "CSS"]
}
})
Field मौजूद है या नहीं?
$exists का इस्तेमाल करें:
db.students.find({
email: { $exists: true }
})
Data Update करें
updateOne()
db.students.updateOne(
{ name: "Rahul" },
{
$set: {
age: 21
}
}
)
कई Documents Update करें
db.students.updateMany(
{ city: "Delhi" },
{
$set: {
country: "India"
}
}
)
$inc Operator
Number को बढ़ाने के लिए:
db.students.updateOne(
{ name: "Rahul" },
{
$inc: {
age: 1
}
}
)
$unset Operator
किसी Field को हटाने के लिए:
db.students.updateOne(
{ name: "Rahul" },
{
$unset: {
email: ""
}
}
)
$push Operator
Array में नया Item जोड़ने के लिए:
db.students.updateOne(
{ name: "Rahul" },
{
$push: {
skills: "MongoDB"
}
}
)
$addToSet Operator
Duplicate Value से बचते हुए Array में Item जोड़ने के लिए:
db.students.updateOne(
{ name: "Rahul" },
{
$addToSet: {
skills: "MongoDB"
}
}
)
$pull Operator
Array से Item हटाने के लिए:
db.students.updateOne(
{ name: "Rahul" },
{
$pull: {
skills: "MongoDB"
}
}
)
Document Delete करें
एक Document:
db.students.deleteOne({
name: "Rahul"
})
कई Documents:
db.students.deleteMany({
city: "Delhi"
})
⚠️ Note
deleteMany() चलाने से पहले Filter को जरूर Check करें।
यह Command कई Documents Delete कर सकती है।
अगर Filter खाली है:
db.students.deleteMany({})
तो Collection के सभी Documents Delete हो सकते हैं।
Sorting
Age के हिसाब से छोटे से बड़े:
db.students.find().sort({
age: 1
})
बड़े से छोटे:
db.students.find().sort({
age: -1
})
Limit
सिर्फ 5 Documents:
db.students.find().limit(5)
Skip
पहले 5 Documents छोड़कर:
db.students.find().skip(5)
Sort + Limit
db.students
.find()
.sort({ age: -1 })
.limit(5)
यह सबसे ज्यादा Age वाले 5 Students दिखा सकता है।
Projection
अगर आपको सिर्फ कुछ Fields चाहिए:
db.students.find(
{},
{
name: 1,
city: 1
}
)
_id छिपाना
db.students.find(
{},
{
name: 1,
city: 1,
_id: 0
}
)
Count
Documents की संख्या देखने के लिए:
db.students.countDocuments()
किसी Filter के साथ:
db.students.countDocuments({
city: "Delhi"
})
Index क्या है?
Index Database में Data को जल्दी खोजने में मदद करता है।
अगर Collection बहुत बड़ी है, तो सही Index Search को तेज़ बनाने में मदद कर सकता है।
Index देखें
db.students.getIndexes()
Index बनाएँ
db.students.createIndex({
name: 1
})
Index हटाएँ
db.students.dropIndex("name_1")
Unique Index
अगर किसी Field में Duplicate Value नहीं चाहिए:
db.users.createIndex(
{ email: 1 },
{ unique: true }
)
MongoDB Aggregation क्या है?
Aggregation का इस्तेमाल Data को Filter, Group, Sort और Calculate करने के लिए किया जाता है।
इसे आसान भाषा में ऐसे समझें:
Documents
↓
Filter
↓
Group
↓
Calculate
↓
Sort
↓
Result
Aggregation में अलग-अलग Stages का इस्तेमाल किया जाता है।
$match
Data Filter करने के लिए:
db.students.aggregate([
{
$match: {
city: "Delhi"
}
}
])
$group
Data को Group करने के लिए:
db.students.aggregate([
{
$group: {
_id: "$city",
total: {
$sum: 1
}
}
}
])
यह हर City में कितने Students हैं, यह निकालने में मदद कर सकता है।
$sum
Total निकालने के लिए:
{
$sum: "$salary"
}
$avg
Average निकालने के लिए:
{
$avg: "$salary"
}
$min
सबसे छोटी Value:
{
$min: "$salary"
}
$max
सबसे बड़ी Value:
{
$max: "$salary"
}
$sort
Aggregation में Sorting:
db.students.aggregate([
{
$sort: {
age: -1
}
}
])
$limit
db.students.aggregate([
{
$limit: 10
}
])
$project
Result में कौन-से Fields दिखाने हैं:
db.students.aggregate([
{
$project: {
name: 1,
city: 1
}
}
])
$lookup
दो Collections से Data जोड़ने के लिए $lookup इस्तेमाल किया जा सकता है।
db.orders.aggregate([
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user"
}
}
])
Aggregation का पूरा Example
मान लीजिए हमारे पास Students हैं और हमें Delhi के Students की Average Age निकालनी है:
db.students.aggregate([
{
$match: {
city: "Delhi"
}
},
{
$group: {
_id: "$city",
averageAge: {
$avg: "$age"
}
}
}
])
MongoDB में Common Operators
| Operator | काम |
|---|---|
$eq | बराबर |
$ne | बराबर नहीं |
$gt | बड़ा |
$gte | बड़ा या बराबर |
$lt | छोटा |
$lte | छोटा या बराबर |
$in | List में मौजूद |
$nin | List में मौजूद नहीं |
$and | सभी Conditions |
$or | कोई एक Condition |
$set | Field बदलना |
$unset | Field हटाना |
$inc | Number बढ़ाना |
$push | Array में जोड़ना |
$pull | Array से हटाना |
$exists | Field Check करना |
MongoDB के Common Commands
| Command | काम |
|---|---|
show dbs | Databases दिखाना |
use database | Database चुनना |
show collections | Collections देखना |
db | Current Database देखना |
insertOne() | एक Document जोड़ना |
insertMany() | कई Documents जोड़ना |
find() | Documents देखना |
findOne() | एक Document देखना |
updateOne() | एक Document बदलना |
updateMany() | कई Documents बदलना |
deleteOne() | एक Document हटाना |
deleteMany() | कई Documents हटाना |
createIndex() | Index बनाना |
dropIndex() | Index हटाना |
aggregate() | Data पर Aggregation करना |
Node.js के साथ MongoDB
MongoDB को Node.js Project में जोड़ने के लिए MongoDB Driver या Mongoose जैसे Tools इस्तेमाल किए जा सकते हैं।
Mongoose Install:
npm install mongoose
Basic Connection:
const mongoose = require("mongoose");
mongoose.connect("YOUR_MONGODB_URL")
.then(() => {
console.log("MongoDB Connected");
})
.catch((error) => {
console.log(error);
});
MongoDB Security के जरूरी Tips
Database के साथ काम करते समय Security का ध्यान रखना बहुत जरूरी है।
Strong Password रखें।
Database Credentials Public न करें।
Connection String को Code में सीधे न रखें।
.envFile का इस्तेमाल करें।User Permissions सही रखें।
जरूरी Data का Backup रखें।
Production Database पर बिना सोचे Delete Commands न चलाएँ।
💡 मेरी सलाह
MongoDB सीखते समय सिर्फ Commands याद करने की कोशिश न करें।
एक छोटा सा Project बनाइए, जैसे:
Student Management System
उसमें:
Students Collection बनाएँ।
Student Add करें।
Student Search करें।
Student Update करें।
Student Delete करें।
City के हिसाब से Filter करें।
Age के हिसाब से Sort करें।
Aggregation से Average Age निकालें।
ऐसा Project बनाने से MongoDB के बहुत सारे Concepts एक साथ समझ आ जाएंगे।
⚠️ Note
MongoDB का Syntax अलग-अलग Versions और Tools में थोड़ा अलग दिखाई दे सकता है। इसके अलावा कुछ पुराने Commands की जगह नए Commands इस्तेमाल किए जाते हैं। इसलिए किसी पुराने Tutorial को Follow करते समय MongoDB Version जरूर Check करें।
MongoDB सीखने का आसान रास्ता
MongoDB Basics
↓
Database & Collection
↓
CRUD Operations
↓
Query Operators
↓
Sorting & Filtering
↓
Indexes
↓
Aggregation
↓
MongoDB + Node.js
↓
Real Project
💡 Pro Tip
MongoDB की Queries को याद करने से ज्यादा जरूरी है उनका Logic समझना।
उदाहरण के लिए:
db.students.find({
age: { $gt: 18 }
})
यहाँ $gt का मतलब Greater Than है।
जब Operators का मतलब समझ आ जाता है, तो नई Query लिखना काफी आसान हो जाता है।
Quick Summary
| Topic | क्या सीखें |
|---|---|
| Basics | Database, Collection, Document |
| CRUD | Insert, Find, Update, Delete |
| Operators | $gt, $lt, $in, $or |
| Arrays | $push, $pull, $addToSet |
| Sorting | sort() |
| Pagination | skip(), limit() |
| Index | createIndex() |
| Aggregation | $match, $group, $sort |
| Connection | Node.js + Mongoose |
Conclusion
MongoDB एक Powerful और Flexible NoSQL Database है। शुरुआत में इसका Document-based Structure थोड़ा अलग लग सकता है, लेकिन इसके Basic Commands समझने के बाद Data के साथ काम करना काफी आसान हो जाता है।
अगर आप MongoDB सीख रहे हैं, तो पहले CRUD Operations अच्छी तरह सीखें। उसके बाद Operators, Sorting, Indexes और आखिर में Aggregation पर जाएँ।
सबसे जरूरी बात यह है कि सिर्फ Cheat Sheet पढ़ने से MongoDB पूरी तरह याद नहीं होगी। खुद Database बनाइए, Documents Insert कीजिए और अलग-अलग Queries चलाकर Practice कीजिए।
यही तरीका MongoDB को जल्दी और अच्छे से सीखने में आपकी मदद करेगा।
FAQs
1. MongoDB क्या है?
MongoDB एक NoSQL Document Database है, जिसमें Data को Collections और Documents के रूप में रखा जाता है।
2. MongoDB और MySQL में क्या अंतर है?
MySQL एक Relational Database है, जिसमें Tables और Rows का इस्तेमाल होता है। MongoDB एक Document Database है, जिसमें Collections और Documents का इस्तेमाल होता है।
3. MongoDB में Table को क्या कहते हैं?
MongoDB में SQL Table के समान काम करने वाली चीज़ को Collection कहा जाता है।
4. MongoDB में Row को क्या कहते हैं?
MongoDB में Row के समान Data Unit को Document कहा जाता है।
5. MongoDB में find() का क्या काम है?
find() Collection में मौजूद Documents को खोजने और दिखाने के लिए इस्तेमाल होता है।
6. $gt और $lt क्या हैं?
$gt का मतलब Greater Than और $lt का मतलब Less Than है। इनका इस्तेमाल Values को Compare करने के लिए किया जाता है।
7. MongoDB में Aggregation क्या है?
Aggregation का इस्तेमाल Data को Filter, Group, Sort और Calculate करने के लिए किया जाता है।
8. MongoDB में Index क्यों बनाया जाता है?
Index सही जगह पर बनाया जाए तो Database को Data जल्दी खोजने में मदद मिल सकती है।
9. क्या MongoDB को Node.js के साथ इस्तेमाल कर सकते हैं?
हाँ। MongoDB को Node.js के साथ MongoDB Driver या Mongoose जैसे Tools की मदद से इस्तेमाल किया जा सकता है।
10. Beginner को MongoDB में सबसे पहले क्या सीखना चाहिए?
सबसे पहले Database, Collection, Document और CRUD Commands सीखें। उसके बाद Query Operators और Aggregation की Practice करें।
🎬 Need help? Click the button below to watch the complete step-by-step video guide tutorial!
📺 Watch Full Video Guide