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
6 changes: 6 additions & 0 deletions website/docs/aggregation operations/_category_.yml
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
label: Aggregation operations
position: 6
link:
description: >
This section details aggregation operations in FerretDB, including aggregation commands, stages, and operators
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
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.
chilagrow marked this conversation as resolved.
Show resolved Hide resolved

![aggregation stages](../../static/img/docs/aggregation-stages.jpg)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

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 `$count` => `$group` => `$sort` stage.
Fashander marked this conversation as resolved.
Show resolved Hide resolved

In the pipeline, a complex query is broken down into separate stages where the record goes through a series of transformations until it finally produces the desired result.

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

## Aggregation commands

Aggregation commands are top-level commands used for aggregating data, and are typically either via the `aggregate`, `count`, or `distinct` command.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

:::note
Aggregation pipeline stages are only available for the `aggregate` command, and not for the `count` or `distinct` command.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
:::

### `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.

```js
// Aggregation pipeline to perform aggregation operations on a collection
db.collection.aggregate([
// Stage 1: Matching documents based on a specific field and value
{ $match: { field: value } },
// Stage 2: Grouping documents by the "category" field and calculating the sum of the "quantity" field
{ $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")
```
18 changes: 18 additions & 0 deletions website/docs/aggregation operations/aggregation-stages.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
sidebar_position: 2
slug: /aggregation-stages/
---

# Aggregation stages

Aggregation stages are a series of one or more processes in a pipeline that acts upon the returned result of the previous stage, starting with the input documents.

| Supported aggregation stages | Description |
Fashander marked this conversation as resolved.
Show resolved Hide resolved
| ---------------------------- | ----------------------------------------------------------------------------------------------------- |
| `$count` | Returns the count of all matched documents in a specified query |
| `$group` | Groups documents based on specific value or expression and returns a single document for each group |
| `$limit` | Passes a limit on the documents passed to the next stage |
Fashander marked this conversation as resolved.
Show resolved Hide resolved
| `$match` | Acts as a `find` operation by only returning documents that match a specified query to the next stage |
| `$skip` | Skips a specified `n` number of documents and passes the rest to the next stage |
| `$sort` | Sorts and returns all the documents based on a specified order |
| `$unwind` | Deconstructs and returns a document for every element in an array field |
2 changes: 1 addition & 1 deletion website/docs/configuration/_category_.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
label: Configuration
position: 8
position: 9
link:
type: generated-index
description: >
Expand Down
2 changes: 1 addition & 1 deletion website/docs/contributing/_category_.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
label: Contributing to FerretDB
position: 13
position: 14
link:
type: generated-index
description: >
Expand Down
2 changes: 1 addition & 1 deletion website/docs/diff.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 9
sidebar_position: 10
slug: /diff/ # referenced in README.md and beacon
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/indexes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 7
sidebar_position: 8
---

# Indexes
Expand Down
2 changes: 1 addition & 1 deletion website/docs/pushdown.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 6
sidebar_position: 7
hide_table_of_contents: true
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/reference/_category_.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
label: Reference
position: 12
position: 13
link:
type: generated-index
description: >
Expand Down
2 changes: 1 addition & 1 deletion website/docs/security.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 11
sidebar_position: 12
slug: /security/ # referenced in README.md
description: TLS and authentication
---
Expand Down
2 changes: 1 addition & 1 deletion website/docs/telemetry.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 10
sidebar_position: 11
slug: /telemetry/ # referenced in many places; must not change
---

Expand Down
3 changes: 3 additions & 0 deletions website/static/img/docs/aggregation-stages.jpg
Fashander marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.