From da820f251cd1b298276931e4de1ac43b12e30b40 Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Fri, 19 May 2023 08:44:14 +0100 Subject: [PATCH 1/8] Add aggregate command --- .../aggregation operations/_category_.yml | 7 +++ .../aggregation-stages.md | 17 ++++++ website/docs/aggregation operations/index.md | 60 +++++++++++++++++++ .../static/img/docs/aggregation-stages.jpg | 3 + 4 files changed, 87 insertions(+) create mode 100644 website/docs/aggregation operations/_category_.yml create mode 100644 website/docs/aggregation operations/aggregation-stages.md create mode 100644 website/docs/aggregation operations/index.md create mode 100644 website/static/img/docs/aggregation-stages.jpg diff --git a/website/docs/aggregation operations/_category_.yml b/website/docs/aggregation operations/_category_.yml new file mode 100644 index 000000000000..c5c89df288d4 --- /dev/null +++ b/website/docs/aggregation operations/_category_.yml @@ -0,0 +1,7 @@ +--- +label: Aggregation Operations +position: 6 +link: + type: generated-index + description: > + This section details aggregation operations in FerretDB,including aggregation commands, stages, and operators diff --git a/website/docs/aggregation operations/aggregation-stages.md b/website/docs/aggregation operations/aggregation-stages.md new file mode 100644 index 000000000000..d688150820c1 --- /dev/null +++ b/website/docs/aggregation operations/aggregation-stages.md @@ -0,0 +1,17 @@ +--- +sidebar_position: 2 +--- + +# 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 | +| ---------------------------- | ----------------------------------------------------------------------------------------------------- | +| `$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 | +| `$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 | diff --git a/website/docs/aggregation operations/index.md b/website/docs/aggregation operations/index.md new file mode 100644 index 000000000000..aec572386de3 --- /dev/null +++ b/website/docs/aggregation operations/index.md @@ -0,0 +1,60 @@ +--- +sidebar_position: 1 +--- + +# Aggregation operations + +Here we should mention the differences between the commands, stages, and operators, and what they are all used for. + +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) + +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. + +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. + +### `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" } } } +]) +``` + +See aggregation pipelines for more details on the various stages. + +### `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") +``` diff --git a/website/static/img/docs/aggregation-stages.jpg b/website/static/img/docs/aggregation-stages.jpg new file mode 100644 index 000000000000..878ad5231f7a --- /dev/null +++ b/website/static/img/docs/aggregation-stages.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ff1c56b54848239b01cd233507aadaa1bd7e766c935a8e720a9881ebb2266cfd +size 194622 From 70763ef794fb03061956700015358714b53c2386 Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Fri, 19 May 2023 11:19:49 +0100 Subject: [PATCH 2/8] Add aggregate command --- website/docs/aggregation operations/_category_.yml | 3 +-- ...{index.md => aggregation-pipeline-and-commands.md} | 11 +++-------- .../docs/aggregation operations/aggregation-stages.md | 1 + website/docs/configuration/_category_.yml | 2 +- website/docs/contributing/_category_.yml | 2 +- website/docs/diff.md | 2 +- website/docs/indexes.md | 2 +- website/docs/pushdown.md | 2 +- website/docs/reference/_category_.yml | 2 +- website/docs/security.md | 2 +- website/docs/telemetry.md | 2 +- 11 files changed, 13 insertions(+), 18 deletions(-) rename website/docs/aggregation operations/{index.md => aggregation-pipeline-and-commands.md} (87%) diff --git a/website/docs/aggregation operations/_category_.yml b/website/docs/aggregation operations/_category_.yml index c5c89df288d4..58d3c7941648 100644 --- a/website/docs/aggregation operations/_category_.yml +++ b/website/docs/aggregation operations/_category_.yml @@ -1,7 +1,6 @@ --- -label: Aggregation Operations +label: Aggregation operations position: 6 link: - type: generated-index description: > This section details aggregation operations in FerretDB,including aggregation commands, stages, and operators diff --git a/website/docs/aggregation operations/index.md b/website/docs/aggregation operations/aggregation-pipeline-and-commands.md similarity index 87% rename from website/docs/aggregation operations/index.md rename to website/docs/aggregation operations/aggregation-pipeline-and-commands.md index aec572386de3..f95c1b5fd55d 100644 --- a/website/docs/aggregation operations/index.md +++ b/website/docs/aggregation operations/aggregation-pipeline-and-commands.md @@ -1,10 +1,9 @@ --- sidebar_position: 1 +slug: /aggregation-pipeline-and-commands/ --- -# Aggregation operations - -Here we should mention the differences between the commands, stages, and operators, and what they are all used for. +# 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. @@ -16,7 +15,7 @@ As shown above, the documents pass through the pipeline with the result of the p 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. +This section of the documentation will focus on [aggregation commands](#aggregation-commands), [aggregation stages](../aggregation-stages), and aggregation operators. ## Aggregation commands @@ -28,9 +27,7 @@ The `aggregate` command is used for performing aggregation operations on a colle 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 } }, @@ -39,8 +36,6 @@ db.collection.aggregate([ ]) ``` -See aggregation pipelines for more details on the various stages. - ### `count` The `count` command displays the number of documents returned by a specific query. diff --git a/website/docs/aggregation operations/aggregation-stages.md b/website/docs/aggregation operations/aggregation-stages.md index d688150820c1..c5313e694dc7 100644 --- a/website/docs/aggregation operations/aggregation-stages.md +++ b/website/docs/aggregation operations/aggregation-stages.md @@ -1,5 +1,6 @@ --- sidebar_position: 2 +slug: /aggregation-stages/ --- # Aggregation stages diff --git a/website/docs/configuration/_category_.yml b/website/docs/configuration/_category_.yml index 8457a36691a2..a1f39560e4e8 100644 --- a/website/docs/configuration/_category_.yml +++ b/website/docs/configuration/_category_.yml @@ -1,6 +1,6 @@ --- label: Configuration -position: 8 +position: 9 link: type: generated-index description: > diff --git a/website/docs/contributing/_category_.yml b/website/docs/contributing/_category_.yml index 8d7315adee3c..b8875e15fa87 100644 --- a/website/docs/contributing/_category_.yml +++ b/website/docs/contributing/_category_.yml @@ -1,6 +1,6 @@ --- label: Contributing to FerretDB -position: 13 +position: 14 link: type: generated-index description: > diff --git a/website/docs/diff.md b/website/docs/diff.md index b42142f1c6c4..85251a499a61 100644 --- a/website/docs/diff.md +++ b/website/docs/diff.md @@ -1,5 +1,5 @@ --- -sidebar_position: 9 +sidebar_position: 10 slug: /diff/ # referenced in README.md and beacon --- diff --git a/website/docs/indexes.md b/website/docs/indexes.md index 4e1a83c75910..e5b55c7171e6 100644 --- a/website/docs/indexes.md +++ b/website/docs/indexes.md @@ -1,5 +1,5 @@ --- -sidebar_position: 7 +sidebar_position: 8 --- # Indexes diff --git a/website/docs/pushdown.md b/website/docs/pushdown.md index aea99abc5081..ffaef8f41300 100644 --- a/website/docs/pushdown.md +++ b/website/docs/pushdown.md @@ -1,5 +1,5 @@ --- -sidebar_position: 6 +sidebar_position: 7 hide_table_of_contents: true --- diff --git a/website/docs/reference/_category_.yml b/website/docs/reference/_category_.yml index 92aa530b15f7..74d90ba245fd 100644 --- a/website/docs/reference/_category_.yml +++ b/website/docs/reference/_category_.yml @@ -1,6 +1,6 @@ --- label: Reference -position: 12 +position: 13 link: type: generated-index description: > diff --git a/website/docs/security.md b/website/docs/security.md index 2b00a36f98f3..f35a88827780 100644 --- a/website/docs/security.md +++ b/website/docs/security.md @@ -1,5 +1,5 @@ --- -sidebar_position: 11 +sidebar_position: 12 slug: /security/ # referenced in README.md description: TLS and authentication --- diff --git a/website/docs/telemetry.md b/website/docs/telemetry.md index 65f17bd67dfb..d2459257ccfe 100644 --- a/website/docs/telemetry.md +++ b/website/docs/telemetry.md @@ -1,5 +1,5 @@ --- -sidebar_position: 10 +sidebar_position: 11 slug: /telemetry/ # referenced in many places; must not change --- From 9bc63c18c9e6013b4055f1d3daa43f68dbc16da9 Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Tue, 23 May 2023 08:52:28 +0100 Subject: [PATCH 3/8] Update website/docs/aggregation operations/_category_.yml Co-authored-by: Chi Fujii --- website/docs/aggregation operations/_category_.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/aggregation operations/_category_.yml b/website/docs/aggregation operations/_category_.yml index 58d3c7941648..dd5fb50676e5 100644 --- a/website/docs/aggregation operations/_category_.yml +++ b/website/docs/aggregation operations/_category_.yml @@ -3,4 +3,4 @@ label: Aggregation operations position: 6 link: description: > - This section details aggregation operations in FerretDB,including aggregation commands, stages, and operators + This section details aggregation operations in FerretDB, including aggregation commands, stages, and operators From 17296a61d830e0732d0c0be061746f09f8e56d4c Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Tue, 23 May 2023 09:26:50 +0100 Subject: [PATCH 4/8] update docs --- .../aggregation-pipeline-and-commands.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/docs/aggregation operations/aggregation-pipeline-and-commands.md b/website/docs/aggregation operations/aggregation-pipeline-and-commands.md index f95c1b5fd55d..cebf992c54f8 100644 --- a/website/docs/aggregation operations/aggregation-pipeline-and-commands.md +++ b/website/docs/aggregation operations/aggregation-pipeline-and-commands.md @@ -21,6 +21,10 @@ This section of the documentation will focus on [aggregation commands](#aggregat Aggregation commands are top-level commands used for aggregating data, and are typically either via the `aggregate`, `count`, or `distinct` command. +:::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. From 66622220680084b90720a76b286b41dcc54acf29 Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Thu, 25 May 2023 10:08:59 +0100 Subject: [PATCH 5/8] Update docs --- .../aggregation-pipeline-and-commands.md | 48 ++++++++++++++++++- .../aggregation-stages.md | 2 +- .../static/img/docs/aggregation-stages.jpg | 4 +- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/website/docs/aggregation operations/aggregation-pipeline-and-commands.md b/website/docs/aggregation operations/aggregation-pipeline-and-commands.md index cebf992c54f8..8c54476ded78 100644 --- a/website/docs/aggregation operations/aggregation-pipeline-and-commands.md +++ b/website/docs/aggregation operations/aggregation-pipeline-and-commands.md @@ -11,9 +11,53 @@ These operations pass through one or more stages, which make up a pipeline. ![aggregation stages](../../static/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 `$count` => `$group` => `$sort` stage. +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. -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. +For example, say you have the following documents in a `sales` collection: + +```js +[ + { "_id": 1, "category": "Electronics", "price": 1000 }, + { "_id": 2, "category": "Electronics", "price": 800 }, + { "_id": 3, "category": "Clothing", "price": 30 }, + { "_id": 4, "category": "Clothing", "price": 50 }, + { "_id": 5, "category": "Home", "price": 1500 }, + { "_id": 6, "category": "Home", "price": 1200 }, + { "_id": 7, "category": "Books", "price": 20 }, + { "_id": 8, "category": "Books", "price": 40 } +] +``` + +A typical aggregation pipeline would look like this: + +```js +db.sales.aggregate([ + { $match: { category: { $ne: "Electronics" } } }, + { + $group: { + _id: "$category", + totalPrice: { $sum: "$price" }, + productCount: { $sum: 1 } + } + }, + { $sort: { totalPrice: -1 } } +]) +``` + +In the pipeline, the complex query is broken down into separate stages where the record goes through a series of transformations until it finally produces the desired result. +First, the `$match` stage filters out all documents where the `category` field is not `Electronics`. +Then, the `$group` stage groups the documents by their `category` and calculates the total price and product count for each of those category. +Finally, the `$sort` stage sorts the documents by the `totalPrice` field in descending order. + +So the above aggregation pipeline operation would return the following result: + +```sh +[ + { _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. diff --git a/website/docs/aggregation operations/aggregation-stages.md b/website/docs/aggregation operations/aggregation-stages.md index c5313e694dc7..c2e17156d819 100644 --- a/website/docs/aggregation operations/aggregation-stages.md +++ b/website/docs/aggregation operations/aggregation-stages.md @@ -11,7 +11,7 @@ Aggregation stages are a series of one or more processes in a pipeline that acts | ---------------------------- | ----------------------------------------------------------------------------------------------------- | | `$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 | +| `$limit` | Limits specific documents and passes the rest to the next stage | | `$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 | diff --git a/website/static/img/docs/aggregation-stages.jpg b/website/static/img/docs/aggregation-stages.jpg index 878ad5231f7a..c4b4b675c64f 100644 --- a/website/static/img/docs/aggregation-stages.jpg +++ b/website/static/img/docs/aggregation-stages.jpg @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ff1c56b54848239b01cd233507aadaa1bd7e766c935a8e720a9881ebb2266cfd -size 194622 +oid sha256:0380eb1fa85c07ee5c6a353df846f5205311b212cd29620ba499dd1722166ef0 +size 150276 From 8f3e833ac794d76baf2a8bc3b86c7f7f380cb899 Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Tue, 30 May 2023 10:38:43 +0100 Subject: [PATCH 6/8] Update docs --- .../_category_.yml | 2 +- .../aggregation-pipeline-and-commands.md | 38 +++---------------- .../aggregation-stages.md | 1 - 3 files changed, 7 insertions(+), 34 deletions(-) rename website/docs/{aggregation operations => aggregation-operations}/_category_.yml (83%) rename website/docs/{aggregation operations => aggregation-operations}/aggregation-pipeline-and-commands.md (66%) rename website/docs/{aggregation operations => aggregation-operations}/aggregation-stages.md (98%) diff --git a/website/docs/aggregation operations/_category_.yml b/website/docs/aggregation-operations/_category_.yml similarity index 83% rename from website/docs/aggregation operations/_category_.yml rename to website/docs/aggregation-operations/_category_.yml index dd5fb50676e5..0f49f7e1a137 100644 --- a/website/docs/aggregation operations/_category_.yml +++ b/website/docs/aggregation-operations/_category_.yml @@ -1,5 +1,5 @@ --- -label: Aggregation operations +label: Aggregation Operations position: 6 link: description: > diff --git a/website/docs/aggregation operations/aggregation-pipeline-and-commands.md b/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md similarity index 66% rename from website/docs/aggregation operations/aggregation-pipeline-and-commands.md rename to website/docs/aggregation-operations/aggregation-pipeline-and-commands.md index 8c54476ded78..7de547fdebcc 100644 --- a/website/docs/aggregation operations/aggregation-pipeline-and-commands.md +++ b/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md @@ -1,6 +1,5 @@ --- sidebar_position: 1 -slug: /aggregation-pipeline-and-commands/ --- # Aggregation pipeline and commands @@ -8,7 +7,7 @@ slug: /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. @@ -51,7 +50,7 @@ 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 }, @@ -59,20 +58,13 @@ So the above aggregation pipeline operation would return the following result: ] ``` -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 @@ -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") -``` diff --git a/website/docs/aggregation operations/aggregation-stages.md b/website/docs/aggregation-operations/aggregation-stages.md similarity index 98% rename from website/docs/aggregation operations/aggregation-stages.md rename to website/docs/aggregation-operations/aggregation-stages.md index c2e17156d819..3c1fb76552ad 100644 --- a/website/docs/aggregation operations/aggregation-stages.md +++ b/website/docs/aggregation-operations/aggregation-stages.md @@ -1,6 +1,5 @@ --- sidebar_position: 2 -slug: /aggregation-stages/ --- # Aggregation stages From c13fac4dbd1ad41a517857d0c771c62b5d7e03ea Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Tue, 30 May 2023 21:16:51 +0100 Subject: [PATCH 7/8] update with format --- .../aggregation-pipeline-and-commands.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md b/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md index 7de547fdebcc..f75a84e33e3b 100644 --- a/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md +++ b/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md @@ -15,15 +15,15 @@ As shown above, the documents pass through the pipeline with the result of the p For example, say you have the following documents in a `sales` collection: ```js -[ - { "_id": 1, "category": "Electronics", "price": 1000 }, - { "_id": 2, "category": "Electronics", "price": 800 }, - { "_id": 3, "category": "Clothing", "price": 30 }, - { "_id": 4, "category": "Clothing", "price": 50 }, - { "_id": 5, "category": "Home", "price": 1500 }, - { "_id": 6, "category": "Home", "price": 1200 }, - { "_id": 7, "category": "Books", "price": 20 }, - { "_id": 8, "category": "Books", "price": 40 } +;[ + { _id: 1, category: 'Electronics', price: 1000 }, + { _id: 2, category: 'Electronics', price: 800 }, + { _id: 3, category: 'Clothing', price: 30 }, + { _id: 4, category: 'Clothing', price: 50 }, + { _id: 5, category: 'Home', price: 1500 }, + { _id: 6, category: 'Home', price: 1200 }, + { _id: 7, category: 'Books', price: 20 }, + { _id: 8, category: 'Books', price: 40 } ] ``` @@ -31,11 +31,11 @@ A typical aggregation pipeline would look like this: ```js db.sales.aggregate([ - { $match: { category: { $ne: "Electronics" } } }, + { $match: { category: { $ne: 'Electronics' } } }, { $group: { - _id: "$category", - totalPrice: { $sum: "$price" }, + _id: '$category', + totalPrice: { $sum: '$price' }, productCount: { $sum: 1 } } }, @@ -72,6 +72,6 @@ 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" } } } + { $group: { _id: '$category', total: { $sum: '$quantity' } } } ]) ``` From 78762ffa630c24061b84db514d4e5993bd604832 Mon Sep 17 00:00:00 2001 From: Alexander Tobi Fashakin Date: Thu, 1 Jun 2023 09:25:00 +0100 Subject: [PATCH 8/8] update with format --- .../aggregation-pipeline-and-commands.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md b/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md index f75a84e33e3b..129926ec89b0 100644 --- a/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md +++ b/website/docs/aggregation-operations/aggregation-pipeline-and-commands.md @@ -12,10 +12,10 @@ These operations pass through one or more stages, which make up a pipeline. 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. -For example, say you have the following documents in a `sales` collection: +For example, insert the following documents in a `sales` collection: ```js -;[ +db.sales.insertMany([ { _id: 1, category: 'Electronics', price: 1000 }, { _id: 2, category: 'Electronics', price: 800 }, { _id: 3, category: 'Clothing', price: 30 }, @@ -24,7 +24,7 @@ For example, say you have the following documents in a `sales` collection: { _id: 6, category: 'Home', price: 1200 }, { _id: 7, category: 'Books', price: 20 }, { _id: 8, category: 'Books', price: 40 } -] +]) ``` A typical aggregation pipeline would look like this: @@ -58,7 +58,7 @@ So the above aggregation pipeline operation would return the following result: ] ``` -This section of the documentation will focus on [`aggregate` command](#aggregate-command), [aggregation stages](../aggregation-stages), and aggregation operators. +This section of the documentation will focus on [`aggregate` command](#aggregate-command), [aggregation stages](./aggregation-stages.md), and aggregation operators. ## `aggregate` command