Skip to content

Commit 05b742f

Browse files
Merge pull request #11 from Meg528/patch-16
Update 4-group.mdx
2 parents e4188e2 + 589c01e commit 05b742f

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

β€Ždocs/50-aggregation/4-group.mdx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import TabItem from "@theme/TabItem";
33

44
# πŸ‘ $group
55

6-
In SQL, the GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
6+
In SQL, the GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country."
77
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
88

99
The `$group` stage in MongoDB’s Aggregation Framework is equivalent to `GROUP BY` in SQL. It allows us to group documents by a specific field and apply aggregate functions like sum, average, count, min, and max.
1010

1111
---
1212

13-
## 🎬 How Does $group Work?\*\*
13+
## 🎬 How does $group work?
1414

1515
The `$group` stage groups documents based on a field and performs calculations on grouped data.
1616

@@ -25,14 +25,14 @@ The `$group` stage groups documents based on a field and performs calculations o
2525
}
2626
```
2727

28-
- `_id`: The field to group by (use `null` to aggregate all documents together).
29-
- `<accumulator>`: An aggregation operator (`$sum`, `$avg`, `$min`, `$max`, `$push`, `$addToSet`, etc.).
28+
- `_id`: The field to group by (use `null` to aggregate all documents together)
29+
- `<accumulator>`: An aggregation operator (`$sum`, `$avg`, `$min`, `$max`, `$push`, `$addToSet`, etc.)
3030

3131
---
3232

33-
## πŸ‘ Example 1: Count the number of Books published every year
33+
## πŸ‘ Example 1: Count the number of books published every year
3434

35-
### **MongoDB Query**
35+
### **MongoDB query**
3636

3737
```js
3838
db.books.aggregate([
@@ -45,15 +45,15 @@ db.books.aggregate([
4545
]);
4646
```
4747

48-
### **Equivalent SQL Query**
48+
### **Equivalent SQL query**
4949

5050
```sql
5151
SELECT year, COUNT(*) AS totalBooks
5252
FROM books
5353
GROUP BY year;
5454
```
5555

56-
### **Sample Output**
56+
### **Sample output**
5757

5858
```json
5959
[
@@ -66,9 +66,9 @@ GROUP BY year;
6666

6767
---
6868

69-
## πŸ‘ Example 2: Without using `$group`- count the number of Books published every year
69+
## πŸ‘ Example 2: Without using `$group`, count the number of books published every year
7070

71-
### MongoDB Query
71+
### MongoDB query
7272

7373
```js
7474
db.books.aggregate([
@@ -78,7 +78,7 @@ db.books.aggregate([
7878
]);
7979
```
8080

81-
### Sample Output remains the same as before
81+
### Sample output remains the same as before
8282

8383
```json
8484
[
@@ -93,7 +93,8 @@ db.books.aggregate([
9393

9494
## πŸ‘ Example 3: Find the total number of pages published every year.
9595

96-
### **MongoDB Query**
96+
97+
### **MongoDB query**
9798

9899
```js
99100
db.books.aggregate([
@@ -106,15 +107,15 @@ db.books.aggregate([
106107
]);
107108
```
108109

109-
### Equivalent SQL Query
110+
### Equivalent SQL query
110111

111112
```sql
112113
SELECT year, SUM(rating) AS totalPages
113114
FROM books
114115
GROUP BY year;
115116
```
116117

117-
### Sample Output
118+
### Sample output
118119

119120
```json
120121
[
@@ -129,7 +130,8 @@ GROUP BY year;
129130

130131
## πŸ‘ Challenge
131132

132-
### πŸ‘ 1. Find the Average Book Rating of all books
133+
134+
### πŸ‘ 1. Find the average book rating of all books
133135

134136
<details>
135137
<summary>Answer</summary>
@@ -146,7 +148,7 @@ GROUP BY year;
146148

147149
</details>
148150

149-
### πŸ‘ 2. Find users with the most number of reviews. Hint: Use the `name` field in the reviews collection.
151+
### πŸ‘ 2. Find users with the most number of reviews (Hint: use the `name` field in the reviews collection)
150152

151153
<details>
152154
<summary>Answer</summary>

0 commit comments

Comments
Β (0)