Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document aggregation operations #2672

Merged
merged 14 commits into from
Jun 2, 2023
Prev Previous commit
Next Next commit
Update docs
  • Loading branch information
Fashander committed May 30, 2023
commit 8f3e833ac794d76baf2a8bc3b86c7f7f380cb899
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
label: Aggregation operations
label: Aggregation Operations
position: 6
link:
description: >
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
sidebar_position: 1
slug: /aggregation-pipeline-and-commands/
---

# Aggregation pipeline and commands

Aggregation operations involve performing various operations on a large number of data records, such as data grouping, sorting, restructuring, or modifying.
These operations pass through one or more stages, which make up a pipeline.

![aggregation stages](../../static/img/docs/aggregation-stages.jpg)
![aggregation stages](/img/docs/aggregation-stages.jpg)

Each stage acts upon the returned documents of the previous stage, starting with the input documents.
As shown above, the documents pass through the pipeline with the result of the previous stage acting as input for the next stage, going from `$match` => `$group` => `$sort` stage.
Expand Down Expand Up @@ -51,28 +50,21 @@ Finally, the `$sort` stage sorts the documents by the `totalPrice` field in desc

So the above aggregation pipeline operation would return the following result:

```sh
```json5
[
{ _id: 'Home', totalPrice: 2700, productCount: 2 },
{ _id: 'Clothing', totalPrice: 80, productCount: 2 },
{ _id: 'Books', totalPrice: 60, productCount: 2 }
]
```

This section of the documentation will focus on [aggregation commands](#aggregation-commands), [aggregation stages](../aggregation-stages), and aggregation operators.
This section of the documentation will focus on [`aggregate` command](#aggregate-command), [aggregation stages](../aggregation-stages), and aggregation operators.

## Aggregation commands
## `aggregate` command

Aggregation commands are top-level commands used for aggregating data, and are typically either via the `aggregate`, `count`, or `distinct` command.
The aggregation command `aggregate` is a top-level command used for aggregating data across various pipeline stages.

:::note
Aggregation pipeline stages are only available for the `aggregate` command, and not for the `count` or `distinct` command.
:::

### `aggregate`

The `aggregate` command is used for performing aggregation operations on a collection.
It lets you specify aggregation operations in a pipeline consisting of one or more stages and operators for transforming and analyzing data, such as grouping, filtering, sorting, projecting, and calculating aggregates.
The command is used for performing aggregation operations on a collection and lets you specify aggregation operations in a pipeline consisting of one or more stages and operators for transforming and analyzing data, such as grouping, filtering, sorting, projecting, and calculating aggregates.

```js
// Aggregation pipeline to perform aggregation operations on a collection
Expand All @@ -83,21 +75,3 @@ db.collection.aggregate([
{ $group: { _id: "$category", total: { $sum: "$quantity" } } }
])
```

### `count`

The `count` command displays the number of documents returned by a specific query.
Returns a `count` as the result.

```js
db.collection.count({ field: value })
```

### `distinct`

The `distinct` command returns unique values for a specified field in a collection.
Returns an array of distinct values for the specified field.

```js
db.collection.distinct("field")
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
sidebar_position: 2
slug: /aggregation-stages/
---

# Aggregation stages
Expand Down