MongoDB

Cover-Img
test1

Created at: September 19, 2026 at 05:17 PM

# MongoDB: Short Notes **What it is** A NoSQL, document-oriented database that stores data as flexible JSON-like documents (BSON) instead of rows and tables. Schema-less by default, so documents in the same collection can have different structures. **Core Terminology** | SQL | MongoDB | |---|---| | Database | Database | | Table | Collection | | Row | Document | | Column | Field | | JOIN | `$lookup` / embedding | **Key Features** - **Flexible schema:** Easy to evolve data models over time. - **BSON format:** Binary JSON, supports extra types like `Date`, `ObjectId`, and `Decimal128`. - **`_id` field:** Every document gets a unique `_id` (an `ObjectId` by default). - **Indexing:** Speeds up queries (single field, compound, text, geospatial). - **Aggregation pipeline:** Multi-stage data processing (`$match`, `$group`, `$sort`, `$project`). - **Replication:** Replica sets provide high availability and failover. - **Sharding:** Horizontal scaling by distributing data across servers. - **Transactions:** Multi-document ACID transactions supported. **Basic CRUD Operations** ```js // Create db.users.insertOne({ name: "Asha", age: 25 }); db.users.insertMany([{ name: "Ravi" }, { name: "Neha" }]); // Read db.users.find({ age: { $gt: 20 } }); db.users.findOne({ name: "Asha" }); // Update db.users.updateOne({ name: "Asha" }, { $set: { age: 26 } }); // Delete db.users.deleteOne({ name: "Ravi" }); ``` **Common Operators** - **Comparison:** `$eq`, `$gt`, `$lt`, `$in`, `$ne` - **Logical:** `$and`, `$or`, `$not` - **Update:** `$set`, `$inc`, `$push`, `$pull` **Data Modeling Tip** - **Embed** related data when it's read together and doesn't grow unbounded. - **Reference** (store IDs) when data is large, shared, or frequently changing. **Aggregation Example** ```js db.orders.aggregate([ { $match: { status: "paid" } }, { $group: { _id: "$customerId", total: { $sum: "$amount" } } }, { $sort: { total: -1 } } ]); ``` **Advantages** Flexible schema, horizontal scalability, fast development, rich query language, and great fit for JavaScript stacks. **Use With Node.js** Use **Mongoose**, an ODM that adds schemas, validation, and models on top of MongoDB. ```bash npm install mongoose ``` **MERN Stack:** MongoDB + Express + React + Node.js. Want notes on Mongoose, Express, or Node.js next?

Comments (0)

Login to comment