notesonly.in

One notebook for every subject — open it anywhere.

Log in

NoSQL Databases and MongoDB Basics With easy examples.

NoSQL databases store data in flexible formats instead of rigid tables — most famously as JSON-like documents. MongoDB is the most popular document database: each record is a document (field-value pairs), documents group into collections, and you query them with a rich, JavaScript-flavoured language.

It matters because real-world data is messy. A catalog where books have page counts, shirts have sizes, and laptops have RAM does not fit one tidy table — you would drown in NULL columns. Documents let each record carry exactly the fields it needs, so your model evolves with your app instead of fighting it.

Key Concepts

  • Document: one record as BSON (binary JSON) — e.g. { name: "Ana", age: 21, hobbies: ["chess", "cycling"] }. Fields nest and hold arrays.
  • Collection: a group of documents, like a table — but documents need not share the same fields.
  • _id: every document's unique identifier, auto-generated if you skip it — the primary-key equivalent.
  • Embedded document: nesting data (address inside user) instead of joining tables — one fetch reads it all.
  • CRUD: insertOne, find, updateOne/updateMany, deleteOne/deleteMany.
  • Query operators: $gt, $lt, $in, $and, $or — e.g. { age: { $gt: 18 } }.
  • Aggregation pipeline: chained stages ($match, $group, $sort) — MongoDB's GROUP BY analytics.

How It Works

MongoDB stores documents in collections with B-tree indexes on chosen fields (_id is automatic). A find() consults a usable index for candidates, then fetches the matches. Writes go to memory plus a write-ahead journal for durability; replica sets copy writes to secondaries for availability. The key design choice is embed vs reference: data always read together (order + line items) gets embedded for single-fetch reads; data shared widely (product details) is referenced by _id to avoid duplication.

Worked Example

students collection: { _id: 1, name: "Ana", city: "Mumbai", marks: 92 }, { _id: 2, name: "Dev", city: "Delhi", marks: 78 }, { _id: 3, name: "Ira", city: "Mumbai", marks: 85 }.

  1. Create: insertOne({ _id: 4, name: "Om", city: "Delhi", marks: 88 }) → 4 documents total.
  2. Read: find({ city: "Mumbai" }) → Ana, Ira — 2 of 4 match (50%).
  3. Operator: find({ marks: { $gt: 80 } }) → Ana (92), Ira (85), Om (88) — 3 documents.
  4. Update: updateMany({ city: "Delhi" }, { $set: { hostel: true } }) → modifiedCount = 2. New field appears on Dev and Om only — schema flexibility live.
  5. Delete: deleteOne({ _id: 2 }) → deletedCount = 1; 3 remain.
  6. Aggregate: average marks per city — Mumbai: (92 + 85) ÷ 2 = 88.5; Delhi: 88 ÷ 1 = 88. One pipeline, exact arithmetic.

Common Misconceptions

  • "NoSQL means no schema." It means flexible schema. Serious apps still validate documents — you evolve without migrations, not without rules.
  • "MongoDB cannot join." The $lookup stage joins collections. The philosophy is "embed first, join when you must".
  • "NoSQL replaces SQL." It complements it. Money ledgers belong in ACID tables; catalogs and event logs shine as documents.

Real-World Applications

  • Content platforms: articles and profiles with varying fields map to documents.
  • Product catalogs: items with wildly different attributes per category.
  • IoT and logging: high-volume, semi-structured sensor and event writes.
  • Mobile backends: user data that changes shape as features are added.

MongoDB trades the rigid table for the flexible document: store what each record needs, embed data you read together, query with expressive filters and pipelines. Reach for it when data is varied and evolving — and keep the relational database for the ledger that must never bend.