đ MongoDB Aggregation Stages
Stages are the building blocks of aggregation pipelines.
đ We saw in the previous page two methods to compose stages to effectively build a pipeline:
- Using the pipeline stages methods
- Using the stages classes directly
Repeating what was described previously:
đĄ Each stage of the aggregation framework also has its own class in the package. And each
Stageclass has a mirror method in thePipeline.
There is actually an asterisk to this. Monggregate does not yet provide an interface to all of the stages provided by MongoDB. It is a work in progress and the list of available stages will grow over time. If you want to contribute, please refer to the contributing guide.
You can see the full list of stages provided by MongoDB here.
đ List of Available Stages In Monggregate
The following table lists the stages that are currently available in Monggregate:
$addFields$bucket$bucketAuto$count$group$limit$lookup$match$merge$out$project$replaceRoot$replaceWith$sample$search$searchMeta$set$skip$sort$sortByCount$unionWith$unset$unwind
đ Usage
đ¯
monggregateaims at providing a simple and intuitive interface to the MongoDB aggregation framework.
Even though, it tries as much as possible to stick by the MongoDB aggregation framework syntax, it also tries to provide alternative ways to reproduce the syntax of other tools that new Mongo users might be more familiar with such as SQL and Pandas.
For example, in the $group stage, the MongoDB aggregation framework expects the grouping field(s) to be provided in the _id key. However, monggregate allows you to provide the grouping field(s) in the by key instead.
Similarly, monggregate pipeline lookup method and Lookup class provide aliases for the orignal MongoDB arguments:
| MongoDB Original Name | Monggregate Original Name | Monggregate Convenient Alias |
|---|---|---|
| from | from | right |
| localField | local_field | left_on |
| foreignField | foreign_field | right_on |
| as | as | name |
âšī¸ Note: The original names of the arguments were converted to snake_case to follow the Python convention. You cannot use the camelCase version of the arguments names here.
You can therefore use any combination of arguments names from the two rightmost columns above to build your stage.
When using the stages methods, you can sometimes omit to name the argument(s). We can for example complete the previous example as follows:
đ The arguments names (
byandvaluerespectively) for thesortandlimitstages are omitted.đĄ Note: Just as the
Pipelineclass provides methods for all stages, theS(Dollar) object provides methods for all MongoDB operators. They serve similar roles in different contexts -Pipelinefor constructing aggregation sequences, andSfor building expressions with operators. For more details on theSobject, see the Operators documentation.
đ ī¸ Operators
You might have noticed in the grouping example how we tell Monggregate to perform operations on the groups.
In the example, we used the $sum and $push operators.
đ For more information about operators, check the next page.