MongoDB Aggregation Framework
51. What is the aggregation framework in MongoDB?
The aggregation framework is a powerful MongoDB tool for data processing and transformation. It operates on a collection's documents and outputs aggregated results, enabling operations like filtering, grouping, sorting, and reshaping data.
Key Features:
- Chain operations using stages in a pipeline.
- Perform data analytics without additional tools.
- Supports a wide range of operators like
$match
,$group
, and$project
.
52. Explain the $match
stage in aggregation.
The $match
stage filters documents based on a specified condition, similar to a find
query.
Example: Find users aged over 30:
db.users.aggregate([
{ $match: { age: { $gt: 30 } } }
])
-
$match
reduces the number of documents passed to subsequent stages, improving efficiency.
53. How does the $group
stage work?
The $group
stage groups documents by a specified field and performs calculations (e.g., sum, average).
Example: Group users by city and calculate the total population per city:
db.users.aggregate([
{ $group: { _id: "$city", totalPopulation: { $sum: 1 } } }
])
-
_id
: The field to group by. - Performs operations like
$sum
,$avg
,$max
, and$min
.
54. What is the purpose of the $project
stage?
The $project
stage reshapes the output documents by including, excluding, or transforming fields.
Example: Show only name
and age
, with a calculated isAdult
field:
db.users.aggregate([
{ $project: { name: 1, age: 1, isAdult: { $gte: ["$age", 18] } } }
])
- Useful for renaming fields, creating new fields, or excluding unwanted fields.
55. How do you perform sorting in the aggregation framework?
The $sort
stage orders documents based on one or more fields.
Syntax:
db.collection.aggregate([{ $sort: { field: order } }])
Example: Sort users by age (ascending) and name (descending):
db.users.aggregate([
{ $sort: { age: 1, name: -1 } }
])
-
1
: Ascending order. -
-1
: Descending order.
56. Explain the $lookup
stage for joins.
The $lookup
stage performs a left outer join with another collection.
Syntax:
{
$lookup: {
from: "otherCollection",
localField: "field1",
foreignField: "field2",
as: "resultField"
}
}
Example: Join orders
with customers
to show customer details for each order:
db.orders.aggregate([
{
$lookup: {
from: "customers",
localField: "customerId",
foreignField: "_id",
as: "customerDetails"
}
}
])
57. What is the $unwind
operator used for?
The $unwind
stage deconstructs an array field into multiple documents, with each array element becoming a separate document.
Example: Expand an array of tags:
db.products.aggregate([
{ $unwind: "$tags" }
])
- If a product has
tags: ["electronics", "gadgets"]
,$unwind
will create two documents, one for each tag.
58. How does the $out
stage work?
The $out
stage writes the aggregation results to a specified collection.
Example: Save aggregated user data to a new collection:
db.users.aggregate([
{ $group: { _id: "$city", totalPopulation: { $sum: 1 } } },
{ $out: "city_population" }
])
- Useful for exporting results for further analysis.
59. What are aggregation pipelines?
Aggregation pipelines are sequences of stages that process documents step by step. Each stage transforms the documents and passes them to the next stage.
Example:
db.users.aggregate([
{ $match: { age: { $gt: 25 } } },
{ $group: { _id: "$city", avgAge: { $avg: "$age" } } },
{ $sort: { avgAge: -1 } }
])
- Combines multiple operations like filtering, grouping, and sorting in one query.
60. Compare aggregation with MapReduce.
Aspect | Aggregation Framework | MapReduce |
---|---|---|
Ease of Use | Declarative and simpler. | Requires custom JavaScript functions. |
Performance | Faster and optimized. | Slower due to JavaScript execution. |
Scalability | Suitable for most use cases. | Better for very large datasets. |
Output | Pipeline-based results. | Results in a new collection. |
Use Case | Aggregating structured data. | Complex computations or custom logic. |
- Use Aggregation Framework for most tasks as it is optimized and simpler.
- Use MapReduce for advanced, custom logic operations.
The aggregation framework is a powerful tool for transforming and analyzing data efficiently within MongoDB, suitable for a variety of analytics and reporting tasks.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
Top comments (0)