Operators
5 methodsCommon query and pipeline operators.
{ $match: <query> }Aggregation stage that filters documents, behaving like a collection find query.
Parameters
| Name | Type | Description |
|---|---|---|
| query | object | Selector document. |
Returns
Stage
Example
mongodb
{ $match: { status: 'active', age: { $gte: 18 } } }{ $group: { _id: <expr>, <field>: { <accumulator>: <expr> } } }Aggregation stage that groups documents by _id expression and applies accumulators per field.
Parameters
| Name | Type | Description |
|---|---|---|
| _id | expression | Group key expression; null groups all documents. |
| accumulator | string | $sum, $avg, $max, $min, $push, $addToSet, etc. |
Returns
Stage
Example
mongodb
{ $group: { _id: '$dept', avgSalary: { $avg: '$salary' } } }{ $project: <specification> }Aggregation stage that passes only specified fields (with optional computed values) downstream.
Parameters
| Name | Type | Description |
|---|---|---|
| specification | object | Field inclusion (1), exclusion (0) or expression. |
Returns
Stage
Example
mongodb
{ $project: { name: 1, age: 1, _id: 0 } }{ $lookup: { from, localField, foreignField, as } }Performs a left outer join to another collection in the same database.
Parameters
| Name | Type | Description |
|---|---|---|
| from | string | Foreign collection name. |
| localField | string | Field from the input documents. |
| foreignField | string | Field from the foreign collection documents. |
| as | string | Output array field name. |
Returns
Stage
Example
mongodb
{ $lookup: { from: 'orders', localField: '_id', foreignField: 'userId', as: 'orders' } }{ $sort: { <field>: 1 | -1 } }Aggregation stage that sorts documents by the given fields; 1 ascending, -1 descending.
Parameters
| Name | Type | Description |
|---|---|---|
| field | string | Field path to sort by. |
Returns
Stage
Example
mongodb
{ $sort: { createdAt: -1 } }