Skip to content

Commit

Permalink
Merge pull request #12 from Meg528/patch-4
Browse files Browse the repository at this point in the history
Update 2-sql-vs-aggregation.mdx
  • Loading branch information
dfreniche authored Sep 17, 2024
2 parents d6cc508 + 358a73e commit a20f920
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions docs/20-what-is-aggregation/2-sql-vs-aggregation.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
description: Let's compare SQL with an Aggregation Pipeline
description: Let's compare SQL with an aggregation pipeline.
---

# 📘 SQL vs Aggregations

## SQL

A SQL query statement usually starts with a `SELECT` where we put a list of the fields we need, then a `FROM` clause to state the table/s (or as in this case the subquery) that will return the rows. We can filter out using `WHERE` and group data using `GROUP`.
A SQL query statement usually starts with a `SELECT` where we put a list of the fields we need, then a `FROM` clause to state the table/s (or in this case, the subquery) that will return the rows. We can filter out using `WHERE` and group data using `GROUP`.

We read this from the inside. If there's too much nesting is not easy to follow.
We read this from the inside. If there's too much nesting, it's not easy to follow.

```SQL
SELECT
Expand All @@ -33,9 +33,9 @@ FROM (
GROUP BY city;
```

## Equivalent MongoDB Aggregation Pipeline
## Equivalent MongoDB aggregation pipeline

Here we pass three stages, one to return one document per element in the `address` array, then we filter out to get only the documents that have a `home` address location and finally we do the grouping. As we'll see, this can be split and tested separately, and resembles our code.
Here we pass three stages: one to return one document per element in the `address` array, and then we filter out to get only the documents that have a `home` address location. Finally, we do the grouping. As we'll see, this can be split and tested separately and resembles our code.

```js
db.customers.aggregate([
Expand All @@ -59,4 +59,4 @@ db.customers.aggregate([

:::info
See also [SQL to Aggregation Mapping Chart](https://www.mongodb.com/docs/manual/reference/sql-aggregation-comparison/)
:::
:::

0 comments on commit a20f920

Please sign in to comment.