Aggregation Pipeline: $project and $unwind
1. Introduction
The MongoDB Aggregation Pipeline is a powerful framework that allows for transforming and processing data. Two key stages of this pipeline are $project
and $unwind
, which are essential for reshaping the data structure.
2. Understanding $project
The $project
stage reshapes each document in the pipeline, allowing you to include, exclude, or add new fields. It is particularly useful for controlling the structure of the output documents.
2.1 Key Concepts
- Includes fields: Specify which fields to return.
- Excludes fields: Specify which fields to omit.
- Computed Fields: Create new fields based on existing ones.
2.2 Example
db.orders.aggregate([
{
$project: {
item: 1,
total: { $multiply: ["$price", "$quantity"] },
_id: 0
}
}
]);
This example projects the item
field and creates a new field total
by multiplying price
and quantity
.
3. Understanding $unwind
The $unwind
stage deconstructs an array field from the input documents to output a document for each element. This is useful when you have arrays and want to normalize them into individual documents.
3.1 Key Concepts
- Flattening Arrays: Each element in the array becomes a separate document.
- Preserving Context: Other fields in the document are preserved.
3.2 Example
db.students.aggregate([
{
$unwind: "$courses"
}
]);
In this example, the courses
array in each student document is unwound, creating a separate document for each course.
4. Best Practices
- Use
$project
early to reduce document size. - Filter data as early as possible using
$match
. - Avoid using
$unwind
on large arrays unless necessary.
5. FAQ
What is the difference between $project and $unwind?
$project
is used to reshape documents by including or excluding fields, while $unwind
is used to convert an array field into multiple documents.
Can I use $project after $unwind?
Yes, you can use $project
after $unwind
to reshape the resulting documents.
What happens to the original document fields when using $unwind?
The original fields are preserved in the resulting documents, but each document will have one element of the array.