MongoDB & NoSQL Notes (English + Simple Hindi)

Complete guide to NoSQL databases – click a topic to jump

Topics of MongoDB & NoSQL Notes

1. Introduction to NoSQL / NoSQL का परिचय

English

What is NoSQL? NoSQL databases are non-relational databases designed for large-scale, distributed data. They offer flexible schemas, horizontal scaling, and are optimized for specific data models (key-value, document, column-family, graph).

Types:

  • Document: MongoDB, CouchDB (data stored as JSON/BSON documents)
  • Key-Value: Redis, DynamoDB
  • Column-family: Cassandra, HBase
  • Graph: Neo4j

CAP Theorem: In distributed systems, you can have at most two of Consistency, Availability, Partition tolerance. NoSQL databases choose accordingly.

सरल हिंदी

NoSQL क्या है? NoSQL डेटाबेस गैर-रिलेशनल डेटाबेस होते हैं, जो बड़े पैमाने पर डेटा के लिए डिज़ाइन किए गए हैं। ये लचीली स्कीमा और क्षैतिज स्केलिंग देते हैं।

प्रकार: दस्तावेज़ (MongoDB), key-value (Redis), column-family (Cassandra), ग्राफ (Neo4j)।

CAP प्रमेय: वितरित सिस्टम में Consistency, Availability, Partition tolerance में से केवल दो ही एक साथ मिल सकते हैं।

// NoSQL databases do not use SQL; they have their own APIs
// MongoDB example (document)
{
    "_id": ObjectId("..."),
    "name": "Raj",
    "address": { "city": "Delhi", "pincode": 110001 }
}
↑ Back to Top

2. MongoDB Introduction / MongoDB का परिचय

English

MongoDB is a leading NoSQL document database. It stores data in flexible, JSON-like documents (BSON format). Key features: high performance, high availability (replica sets), automatic sharding, rich query language.

Terminology: Database → Collection → Document (instead of Database → Table → Row).

सरल हिंदी

MongoDB एक प्रमुख NoSQL डेटाबेस है। यह JSON जैसे दस्तावेज़ों (BSON) में डेटा स्टोर करता है। तेज, स्केलेबल, और लचीला।

शब्दावली: डेटाबेस → कलेक्शन → दस्तावेज़ (SQL में डेटाबेस → टेबल → पंक्ति के बजाय)।

// Sample MongoDB document
{
    "_id": 1,
    "name": "HITCOM",
    "courses": ["C", "Python", "MongoDB"],
    "active": true
}
↑ Back to Top

3. Installation & Setup / इंस्टॉलेशन

English

Install MongoDB Community Edition: On Ubuntu: sudo apt install -y mongodb (or use official repos). On Windows: download MSI installer from mongodb.com.

Start service: sudo systemctl start mongod.

Shell: run mongosh (new shell) or mongo (legacy).

सरल हिंदी

MongoDB इंस्टॉल करना: उबंटू पर sudo apt install mongodb। विंडोज पर mongodb.com से installer।

शुरू करें: sudo systemctl start mongod

शेल चलाएँ: mongosh

$ sudo systemctl status mongod
$ mongosh
test>
↑ Back to Top

4. Databases & Collections / डेटाबेस और कलेक्शन

English

Show databases: show dbs

Switch/Create database: use mydb (creates when first data inserted)

Create collection explicitly: db.createCollection("users")

Show collections: show collections

सरल हिंदी

डेटाबेस देखें: show dbs

डेटाबेस बदलें/बनाएँ: use mydb (पहला डेटा डालने पर बनेगा)

कलेक्शन बनाएँ: db.createCollection("users")

> use school
switched to db school
> db.createCollection("students")
{ ok: 1 }
> show collections
students
↑ Back to Top

5. Documents (BSON) / दस्तावेज़

English

Documents are JSON-like objects with field:value pairs. BSON (Binary JSON) supports additional data types like ObjectId, Date, Binary data, etc.

_id is a mandatory unique identifier; if not provided, MongoDB generates an ObjectId.

सरल हिंदी

दस्तावेज़ JSON जैसी संरचना होती है। _id हर दस्तावेज़ में यूनिक होता है; अगर न दें तो MongoDB ObjectId बना देता है।

{
    "_id": ObjectId("507f1f77bcf86cd799439011"),
    "name": "Priya",
    "age": 24,
    "courses": ["math", "science"],
    "address": { "city": "Mumbai", "zip": 400001 }
}
↑ Back to Top

6. Insert Operations / डेटा डालना

English

db.collection.insertOne() – insert single document

db.collection.insertMany() – insert multiple documents

सरल हिंदी

insertOne() – एक दस्तावेज़ डालें

insertMany() – कई दस्तावेज़ एक साथ डालें

db.students.insertOne({
    name: "Ravi",
    age: 21,
    enrolled: true
});

db.students.insertMany([
    { name: "Anu", age: 22 },
    { name: "Simran", age: 23 }
]);
↑ Back to Top

7. Find (Query) / डेटा ढूँढ़ना

English

db.collection.find() – returns cursor to documents.

findOne() – returns first matching document.

Pass a query document to filter: { field: value }

सरल हिंदी

find() – दस्तावेज़ ढूँढ़ता है।

findOne() – पहला मिलान दस्तावेज़ लौटाता है।

db.students.find()  // all documents
db.students.find({ age: 22 })  // filter
db.students.findOne({ name: "Ravi" })
↑ Back to Top

8. Update Operations / अपडेट करना

English

updateOne(), updateMany(), replaceOne().

Use update operators like $set, $inc, $push to modify fields.

सरल हिंदी

updateOne() – एक दस्तावेज़ बदलें। $set से फ़ील्ड की वैल्यू बदलें, $inc से संख्या बढ़ाएँ।

db.students.updateOne(
    { name: "Ravi" },
    { $set: { age: 22 } }
);

db.students.updateMany(
    { enrolled: true },
    { $inc: { age: 1 } }
);
↑ Back to Top

9. Delete Operations / हटाना

English

deleteOne(), deleteMany().

सरल हिंदी

deleteOne() – एक दस्तावेज़ हटाएँ, deleteMany() – कई हटाएँ।

db.students.deleteOne({ name: "Anu" });
db.students.deleteMany({ age: { $lt: 18 } });
↑ Back to Top

10. Query Operators / क्वेरी ऑपरेटर

English

Comparison: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin

Logical: $and, $or, $not, $nor

Element: $exists, $type

Array: $all, $size, $elemMatch

सरल हिंदी

तुलना: $gt (बड़ा), $lt (छोटा), $in (सूची में), आदि।

db.students.find({ age: { $gt: 20, $lt: 30 } });
db.students.find({ $or: [{age:22}, {name:"Ravi"}] });
db.students.find({ "address.city": "Delhi" });
↑ Back to Top

11. Projection & Sorting / प्रोजेक्शन और सॉर्टिंग

English

Projection: specify which fields to include (1) or exclude (0).

Sorting: sort({ field: 1 }) for ascending, -1 for descending.

limit() and skip() for pagination.

सरल हिंदी

प्रोजेक्शन: कौन से फ़ील्ड दिखाने हैं। सॉर्ट: क्रम लगाना।

db.students.find({}, { name: 1, age: 1, _id: 0 })
db.students.find().sort({ age: -1 }).limit(5)
↑ Back to Top

12. Indexes / इंडेक्स

English

Indexes improve query performance. createIndex({ field: 1 }) (1 ascending, -1 descending).

Compound indexes, unique indexes, TTL indexes, etc.

सरल हिंदी

इंडेक्स से क्वेरी तेज होती है। createIndex({ name: 1 }) नाम पर इंडेक्स बनाना।

db.students.createIndex({ age: 1 })
db.students.getIndexes()
↑ Back to Top

13. Aggregation Framework / एग्रीगेशन

English

Aggregation pipelines process data in stages: $match, $group, $project, $sort, $unwind, etc.

सरल हिंदी

एग्रीगेशन पाइपलाइन से डेटा को कई चरणों में प्रोसेस करते हैं (जैसे $group – समूह बनाना, $sum – जोड़)।

db.students.aggregate([
    { $match: { age: { $gte: 18 } } },
    { $group: { _id: "$age", count: { $sum: 1 } } },
    { $sort: { count: -1 } }
]);
↑ Back to Top

14. Data Modeling (Embedding vs Referencing) / डेटा मॉडलिंग

English

Embedding: store related data inside a single document (good for one-to-few, data accessed together).

Referencing: store links between documents (like foreign keys) – better for large, separate data.

सरल हिंदी

Embedding: संबंधित डेटा एक ही दस्तावेज़ में रखना (जैसे पते के साथ ऑर्डर)। Referencing: अलग-अलग दस्तावेज़ में रखकर ID से लिंक करना।

// Embedding
{
    _id: 1,
    user: "Raj",
    orders: [ { product: "Laptop", qty: 1 } ]
}

// Referencing
{ _id: 1, user: "Raj" }
{ _id: 100, user_id: 1, product: "Laptop" }
↑ Back to Top

15. Replication & Sharding / रेप्लिकेशन और शार्डिंग

English

Replication: Replica sets provide high availability – one primary, multiple secondaries.

Sharding: Horizontal scaling by distributing data across multiple servers using a shard key.

सरल हिंदी

रेप्लिकेशन: डेटा की कॉपी कई सर्वर पर रखना, ताकि एक खराब होने पर दूसरा काम करे। शार्डिंग: डेटा को कई सर्वर पर बाँटना (हॉरिजॉन्टल स्केलिंग)।

// Sharding enables distribution
sh.shardCollection("school.students", { age: 1 })
↑ Back to Top

16. Backup & Restore / बैकअप और रिस्टोर

English

mongodump – create binary backup of database.

mongorestore – restore from dump.

Also mongoexport/mongoimport for JSON/CSV.

सरल हिंदी

mongodump से बैकअप लें, mongorestore से रिस्टोर करें।

mongodump --db school --out /backup/
mongorestore --db school_new /backup/school/
↑ Back to Top

17. Using MongoDB with Drivers (Node.js/Python) / ड्राइवर

English

MongoDB provides official drivers for many languages.

Node.js: npm install mongodb

Python: pip install pymongo

सरल हिंदी

विभिन्न भाषाओं के लिए MongoDB के ड्राइवर हैं। Node.js के लिए mongodb पैकेज, Python के लिए pymongo

// Node.js example
const { MongoClient } = require('mongodb');
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
const db = client.db('school');
const docs = await db.collection('students').find().toArray();
↑ Back to Top

18. MongoDB Atlas (Cloud) / मोंगोडीबी एटलस

English

Atlas is the official cloud database service. Free tier available. Provides automated backups, monitoring, and easy scaling.

सरल हिंदी

Atlas क्लाउड पर MongoDB की सेवा है। फ्री टियर भी है। बैकअप, मॉनिटरिंग अपने आप होती है।

// Connection string format
mongodb+srv://username:password@cluster.mongodb.net/dbname
↑ Back to Top

19. MongoDB vs SQL (Comparison) / तुलना

English

SQLMongoDB
DatabaseDatabase
TableCollection
RowDocument
ColumnField
Primary Key_id
JOINsEmbedding / $lookup (aggregation)

सरल हिंदी

SQL में टेबल, MongoDB में कलेक्शन। SQL में पंक्ति (row), MongoDB में दस्तावेज़ (document)।

-- SQL
SELECT * FROM students WHERE age = 22;

// MongoDB
db.students.find({ age: 22 })
↑ Back to Top

20. Best Practices / सर्वोत्तम तरीके

English

  • Always use indexes for fields you query frequently.
  • Choose appropriate data types (use NumberDecimal for currency).
  • Embed when data is always accessed together; reference for separate entities.
  • Enable authentication and use strong passwords.
  • Monitor performance with `explain()`.
  • Use replica sets for production.

सरल हिंदी

  • जिन फ़ील्ड पर क्वेरी करते हैं, उन पर इंडेक्स बनाएँ।
  • डेटा मॉडल सही चुनें – embedding या referencing।
  • प्रोडक्शन में replica set जरूर उपयोग करें।
// Always check query performance
db.students.find({ age: 22 }).explain("executionStats")
↑ Back to Top