This text is a practical guide to performing basic operations in MongoDB, one of the most popular NoSQL databases for storing and managing data. It focuses on essential topics like CRUD operations (Create, Read, Update, Delete), customizing queries with sorting, pagination, and projections, and more advanced techniques such as the Aggregation Framework. In addition to these, this guide touches on: Using query operators for filtering data (e.g., $eq, $gt, $in), working with nested fields, utilizing logical operators ($and, $or, $not), managing arrays with operators like $push and $unwind, and even performing joins between collections with $lookup. Whether you’re a beginner looking to get started or an experienced user aiming to brush up on key concepts, this guide provides a clear and concise overview of MongoDB's most practical and widely used features. 1. CRUD Operations (Create, Read, Update, Delete) 1.1 Create - Inserting Data insertOne(): Insert a single document. db.collection.insertOne({ name: "John", age: 25 }); insertMany(): Insert multiple documents. db.collection.insertMany([{ name: "Maria" }, { name: "Peter" }]); 1.2 Read - Querying Data find(): Retrieve multiple documents. db.collection.find({ age: { $gte: 18 } }); findOne(): Retrieve a single document. db.collection.findOne({ name: "John" }); Query Operators $eq: Equal to. $ne: Not equal to. $gt, $lt, $gte, $lte: Numeric comparisons. $in, $nin: Check if a value is inside/outside a list. $exists: Check if a field exists. 1.3 Update - Updating Data updateOne(): Update a single document. db.collection.updateOne({ name: "John" }, { $set: { age: 30 } }); updateMany(): Update multiple documents. db.collection.updateMany({ age: { $lt: 18 } }, { $inc: { age: 1 } }); Update Operators $set: Modify fields. $inc: Increment numeric values. $push, $pull: Add/remove elements from arrays. 1.4 Delete - Deleting Data deleteOne(): Delete a single document. db.collection.deleteOne({ name: "Peter" }); deleteMany(): Delete multiple documents. db.collection.deleteMany({ age: { $lt: 18 } }); 2. Customizing Queries 2.1 Field Projection Return specific fields: db.collection.find({}, { name: 1, _id: 0 }); 2.2 Sorting and Pagination Sorting: db.collection.find().sort({ age: 1 }); Pagination: db.collection.find().skip(5).limit(10); 2.3 Regular Expressions Partial search: db.collection.find({ name: /john/i }); 2.4 Querying Nested Fields Query within subfields: db.collection.find({ "address.city": "New York" }); 2.5 Logical Operators $and: db.collection.find({ $and: [{ age: { $gte: 18 } }, { city: "Los Angeles" }] }); $or: db.collection.find({ $or: [{ city: "New York" }, { city: "San Francisco" }] }); 3. Aggregation Framework 3.1 Aggregation Pipeline A sequence of stages to transform data: db.collection.aggregate([ { $match: { age: { $gte: 18 } } }, { $group: { _id: "$city", total: { $sum: 1 } } } ]); 3.2 Key Stages 3.2.1 $match (Filtering Data) Filter documents in the pipeline: { $match: { age: { $gte: 18 } } } 3.2.2 $project (Data Transformation) Create or modify fields: { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } } 3.2.3 $group (Data Grouping) Group by a field and perform calculations: { $group: { _id: "$city", total: { $sum: 1 } } } 3.2.4 Sorting and Limiting Sorting: { $sort: { total: -1 } } Limiting: { $limit: 10 } 3.2.5 Array Manipulation $unwind: Expand arrays into individual documents: { $unwind: "$hobbies" } 3.2.6 $lookup (Relationships) Perform "joins" between collections: { $lookup: { from: "orders", localField: "userId", foreignField: "userId", as: "userOrders" } } 3.2.7 $out (Export Results) Export results to a new collection: { $out: "newCollection" } This text is a practical guide to performing basic operations in MongoDB, one of the most popular NoSQL databases for storing and managing data. It focuses on essential topics like CRUD operations (Create, Read, Update, Delete), customizing queries with sorting, pagination, and projections, and more advanced techniques such as the Aggregation Framework. In addition to these, this guide touches on: Using query operators for filtering data (e.g., $eq, $gt, $in), working with nested fields, utilizing logical operators ($and, $or, $not), managing arrays with operators like $push and $unwind, and even performing joins between collections with $lookup. Whether you’re a beginner looking to get started or an experienced user aiming to brush up on key concepts, this guide provides a clear and concise overview of MongoDB's most practical and widely used features. 1. CRUD Operations (Create, Read, Update, Delete) 1. CRUD Operations (Create, Read, Update, Delete) 1.1 Create - Inserting Data 1.1 Create - Inserting Data insertOne(): Insert a single document. db.collection.insertOne({ name: "John", age: 25 }); insertMany(): Insert multiple documents. db.collection.insertMany([{ name: "Maria" }, { name: "Peter" }]); insertOne(): Insert a single document. db.collection.insertOne({ name: "John", age: 25 }); insertOne() : Insert a single document. insertOne() db.collection.insertOne({ name: "John", age: 25 }); db.collection.insertOne({ name: "John", age: 25 }); insertMany(): Insert multiple documents. db.collection.insertMany([{ name: "Maria" }, { name: "Peter" }]); insertMany() : Insert multiple documents. insertMany() db.collection.insertMany([{ name: "Maria" }, { name: "Peter" }]); db.collection.insertMany([{ name: "Maria" }, { name: "Peter" }]); 1.2 Read - Querying Data 1.2 Read - Querying Data find(): Retrieve multiple documents. db.collection.find({ age: { $gte: 18 } }); findOne(): Retrieve a single document. db.collection.findOne({ name: "John" }); find(): Retrieve multiple documents. db.collection.find({ age: { $gte: 18 } }); find() : Retrieve multiple documents. find() db.collection.find({ age: { $gte: 18 } }); db.collection.find({ age: { $gte: 18 } }); findOne(): Retrieve a single document. db.collection.findOne({ name: "John" }); findOne() : Retrieve a single document. findOne() db.collection.findOne({ name: "John" }); db.collection.findOne({ name: "John" }); Query Operators Query Operators $eq: Equal to. $ne: Not equal to. $gt, $lt, $gte, $lte: Numeric comparisons. $in, $nin: Check if a value is inside/outside a list. $exists: Check if a field exists. $eq : Equal to. $eq $ne : Not equal to. $ne $gt , $lt , $gte , $lte : Numeric comparisons. $gt $lt $gte $lte $in , $nin : Check if a value is inside/outside a list. $in $nin $exists : Check if a field exists. $exists 1.3 Update - Updating Data 1.3 Update - Updating Data updateOne(): Update a single document. db.collection.updateOne({ name: "John" }, { $set: { age: 30 } }); updateMany(): Update multiple documents. db.collection.updateMany({ age: { $lt: 18 } }, { $inc: { age: 1 } }); updateOne(): Update a single document. db.collection.updateOne({ name: "John" }, { $set: { age: 30 } }); updateOne() : Update a single document. updateOne() db.collection.updateOne({ name: "John" }, { $set: { age: 30 } }); db.collection.updateOne({ name: "John" }, { $set: { age: 30 } }); updateMany(): Update multiple documents. db.collection.updateMany({ age: { $lt: 18 } }, { $inc: { age: 1 } }); updateMany() : Update multiple documents. updateMany() db.collection.updateMany({ age: { $lt: 18 } }, { $inc: { age: 1 } }); db.collection.updateMany({ age: { $lt: 18 } }, { $inc: { age: 1 } }); Update Operators Update Operators $set: Modify fields. $inc: Increment numeric values. $push, $pull: Add/remove elements from arrays. $set : Modify fields. $set $inc : Increment numeric values. $inc $push , $pull : Add/remove elements from arrays. $push $pull 1.4 Delete - Deleting Data 1.4 Delete - Deleting Data deleteOne(): Delete a single document. db.collection.deleteOne({ name: "Peter" }); deleteMany(): Delete multiple documents. db.collection.deleteMany({ age: { $lt: 18 } }); deleteOne(): Delete a single document. db.collection.deleteOne({ name: "Peter" }); deleteOne() : Delete a single document. deleteOne() db.collection.deleteOne({ name: "Peter" }); db.collection.deleteOne({ name: "Peter" }); deleteMany(): Delete multiple documents. db.collection.deleteMany({ age: { $lt: 18 } }); deleteMany() : Delete multiple documents. deleteMany() db.collection.deleteMany({ age: { $lt: 18 } }); db.collection.deleteMany({ age: { $lt: 18 } }); 2. Customizing Queries 2. Customizing Queries 2.1 Field Projection 2.1 Field Projection Return specific fields: db.collection.find({}, { name: 1, _id: 0 }); Return specific fields: db.collection.find({}, { name: 1, _id: 0 }); Return specific fields: db.collection.find({}, { name: 1, _id: 0 }); db.collection.find({}, { name: 1, _id: 0 }); 2.2 Sorting and Pagination 2.2 Sorting and Pagination Sorting: db.collection.find().sort({ age: 1 }); Pagination: db.collection.find().skip(5).limit(10); Sorting: db.collection.find().sort({ age: 1 }); Sorting : Sorting db.collection.find().sort({ age: 1 }); db.collection.find().sort({ age: 1 }); Pagination: db.collection.find().skip(5).limit(10); Pagination : Pagination db.collection.find().skip(5).limit(10); db.collection.find().skip(5).limit(10); 2.3 Regular Expressions 2.3 Regular Expressions Partial search: db.collection.find({ name: /john/i }); Partial search: db.collection.find({ name: /john/i }); Partial search: db.collection.find({ name: /john/i }); db.collection.find({ name: /john/i }); 2.4 Querying Nested Fields 2.4 Querying Nested Fields Query within subfields: db.collection.find({ "address.city": "New York" }); Query within subfields: db.collection.find({ "address.city": "New York" }); Query within subfields: db.collection.find({ "address.city": "New York" }); db.collection.find({ "address.city": "New York" }); 2.5 Logical Operators 2.5 Logical Operators $and: db.collection.find({ $and: [{ age: { $gte: 18 } }, { city: "Los Angeles" }] }); $or: db.collection.find({ $or: [{ city: "New York" }, { city: "San Francisco" }] }); $and: db.collection.find({ $and: [{ age: { $gte: 18 } }, { city: "Los Angeles" }] }); $and : $and db.collection.find({ $and: [{ age: { $gte: 18 } }, { city: "Los Angeles" }] }); db.collection.find({ $and: [{ age: { $gte: 18 } }, { city: "Los Angeles" }] }); $or: db.collection.find({ $or: [{ city: "New York" }, { city: "San Francisco" }] }); $or : $or db.collection.find({ $or: [{ city: "New York" }, { city: "San Francisco" }] }); db.collection.find({ $or: [{ city: "New York" }, { city: "San Francisco" }] }); 3. Aggregation Framework 3. Aggregation Framework 3.1 Aggregation Pipeline 3.1 Aggregation Pipeline A sequence of stages to transform data: db.collection.aggregate([ { $match: { age: { $gte: 18 } } }, { $group: { _id: "$city", total: { $sum: 1 } } } ]); A sequence of stages to transform data: db.collection.aggregate([ { $match: { age: { $gte: 18 } } }, { $group: { _id: "$city", total: { $sum: 1 } } } ]); A sequence of stages to transform data: db.collection.aggregate([ { $match: { age: { $gte: 18 } } }, { $group: { _id: "$city", total: { $sum: 1 } } } ]); db.collection.aggregate([ { $match: { age: { $gte: 18 } } }, { $group: { _id: "$city", total: { $sum: 1 } } } ]); 3.2 Key Stages 3.2 Key Stages 3.2.1 $match (Filtering Data) 3.2.1 $match (Filtering Data) Filter documents in the pipeline: { $match: { age: { $gte: 18 } } } Filter documents in the pipeline: { $match: { age: { $gte: 18 } } } Filter documents in the pipeline: { $match: { age: { $gte: 18 } } } { $match: { age: { $gte: 18 } } } 3.2.2 $project (Data Transformation) 3.2.2 $project (Data Transformation) Create or modify fields: { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } } Create or modify fields: { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } } Create or modify fields: { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } } { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } } 3.2.3 $group (Data Grouping) 3.2.3 $group (Data Grouping) Group by a field and perform calculations: { $group: { _id: "$city", total: { $sum: 1 } } } Group by a field and perform calculations: { $group: { _id: "$city", total: { $sum: 1 } } } Group by a field and perform calculations: { $group: { _id: "$city", total: { $sum: 1 } } } { $group: { _id: "$city", total: { $sum: 1 } } } 3.2.4 Sorting and Limiting 3.2.4 Sorting and Limiting Sorting: { $sort: { total: -1 } } Limiting: { $limit: 10 } Sorting: { $sort: { total: -1 } } Sorting : Sorting { $sort: { total: -1 } } { $sort: { total: -1 } } Limiting: { $limit: 10 } Limiting : Limiting { $limit: 10 } { $limit: 10 } 3.2.5 Array Manipulation 3.2.5 Array Manipulation $unwind: Expand arrays into individual documents: { $unwind: "$hobbies" } $unwind: Expand arrays into individual documents: { $unwind: "$hobbies" } $unwind : Expand arrays into individual documents: $unwind { $unwind: "$hobbies" } { $unwind: "$hobbies" } 3.2.6 $lookup (Relationships) 3.2.6 $lookup (Relationships) Perform "joins" between collections: { $lookup: { from: "orders", localField: "userId", foreignField: "userId", as: "userOrders" } } Perform "joins" between collections: { $lookup: { from: "orders", localField: "userId", foreignField: "userId", as: "userOrders" } } Perform "joins" between collections: { $lookup: { from: "orders", localField: "userId", foreignField: "userId", as: "userOrders" } } { $lookup: { from: "orders", localField: "userId", foreignField: "userId", as: "userOrders" } } 3.2.7 $out (Export Results) 3.2.7 $out (Export Results) Export results to a new collection: { $out: "newCollection" } Export results to a new collection: { $out: "newCollection" } Export results to a new collection: { $out: "newCollection" } { $out: "newCollection" }