Complete guide to NoSQL databases – click a topic to jump
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:
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 }
}
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
}
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>
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
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 }
}
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 }
]);
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" })
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 } }
);
deleteOne(), deleteMany().
deleteOne() – एक दस्तावेज़ हटाएँ, deleteMany() – कई हटाएँ।
db.students.deleteOne({ name: "Anu" });
db.students.deleteMany({ age: { $lt: 18 } });
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" });
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)
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()
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 } }
]);
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" }
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 })
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/
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();
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
| SQL | MongoDB |
|---|---|
| Database | Database |
| Table | Collection |
| Row | Document |
| Column | Field |
| Primary Key | _id |
| JOINs | Embedding / $lookup (aggregation) |
SQL में टेबल, MongoDB में कलेक्शन। SQL में पंक्ति (row), MongoDB में दस्तावेज़ (document)।
-- SQL
SELECT * FROM students WHERE age = 22;
// MongoDB
db.students.find({ age: 22 })
// Always check query performance
db.students.find({ age: 22 }).explain("executionStats")