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.
insertOne()
: Insert a single document.
db.collection.insertOne({ name: "John", age: 25 });
insertMany()
: Insert multiple documents.
db.collection.insertMany([{ name: "Maria" }, { name: "Peter" }]);
find()
: Retrieve multiple documents.
db.collection.find({ age: { $gte: 18 } });
findOne()
: Retrieve a single document.
db.collection.findOne({ name: "John" });
$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.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 } });
$set
: Modify fields.$inc
: Increment numeric values.$push
, $pull
: Add/remove elements from arrays.deleteOne()
: Delete a single document.
db.collection.deleteOne({ name: "Peter" });
deleteMany()
: Delete multiple documents.
db.collection.deleteMany({ age: { $lt: 18 } });
Return specific fields:
db.collection.find({}, { name: 1, _id: 0 });
Sorting:
db.collection.find().sort({ age: 1 });
Pagination:
db.collection.find().skip(5).limit(10);
Partial search:
db.collection.find({ name: /john/i });
Query within subfields:
db.collection.find({ "address.city": "New York" });
$and
:
db.collection.find({ $and: [{ age: { $gte: 18 } }, { city: "Los Angeles" }] });
$or
:
db.collection.find({ $or: [{ city: "New York" }, { city: "San Francisco" }] });
A sequence of stages to transform data:
db.collection.aggregate([
{ $match: { age: { $gte: 18 } } },
{ $group: { _id: "$city", total: { $sum: 1 } } }
]);
Filter documents in the pipeline:
{ $match: { age: { $gte: 18 } } }
Create or modify fields:
{ $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }
Group by a field and perform calculations:
{ $group: { _id: "$city", total: { $sum: 1 } } }
Sorting:
{ $sort: { total: -1 } }
Limiting:
{ $limit: 10 }
$unwind: Expand arrays into individual documents:
{ $unwind: "$hobbies" }
Perform "joins" between collections:
{
$lookup: {
from: "orders",
localField: "userId",
foreignField: "userId",
as: "userOrders"
}
}
Export results to a new collection:
{ $out: "newCollection" }