Skip to content

Commit 589c01e

Browse files
Merge branch 'main' into patch-16
2 parents d825ad0 + e4188e2 commit 589c01e

16 files changed

+89
-87
lines changed

README.md

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
# Docusaurus Template Workshop
1+
# SQL to MongoDB Query API
22

3-
This is a template to __create new Lab documentation sites__. Contains info on how to use Docusaurus and is a good starting point.
4-
5-
### Installation, use, how to build, etc.
6-
7-
Everything is covered in the Lab itself: https://mongodb-developer.github.io/docusaurus-workshop/
8-
9-
## Contributing
10-
11-
As `main` is protected, submit a pull request to be reviewed.
12-
13-
## Docusaurus
14-
15-
This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator. It's available on https://mongodb-developer.github.io/docusaurus-workshop/.
16-
17-
### Disclaimer
18-
19-
Use at your own risk; not a supported MongoDB product
3+
Workshop Docs hosted at: https://sql-to-query-api-lab.github.io/sql-to-query-api-lab/docs/category/prerequisites

docs/20-prerequisites/20-prerequisite.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: Setting up your MongoDB Atlas account, importing Library data
66

77
To follow along, you'll need to complete the first two labs of the MongoDB for RDBMS professionals. Which will help you in getting:
88

9-
- MongoDB Atlas Cluster
10-
- Test data. In this case, this is book, author, and review data for a library management system.
9+
- A free MongoDB Atlas Cluster
10+
- Sample data
1111

12-
👐 To get both, open the [intro lab](https://mongodb-developer.github.io/intro-lab/docs/intro) and follow it (only takes 10-15 mins) to get your database ready. Return here when finished!
12+
To get both, open the [intro lab](https://mongodb-developer.github.io/intro-lab/docs/intro) and follow it (only takes 10-15 mins) to get your database ready. Return here when finished!

docs/30-quick-start.mdx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,26 @@ To use work through the exercises, we need to install the official MongoDB GUI:
88

99
## Connect compass to your Atlas cluster
1010

11-
- Insert Add connection page of compass
11+
### 1. Click on the "Add connection" button on the home page
1212

13-
## Select the library database
13+
![](/img/add-connection-compass.png)
1414

15-
- Insert databases list
15+
### 2. Paste your connection string after updating your password and click on the "Save and Connect" button
16+
17+
![](/img/enter-connection-uri-compass.png)
18+
19+
### 3. Select the library database
20+
21+
![](/img/select-db-collection-compass.png)
22+
23+
### 4. Click on the "Open MongoDB shell" button
24+
25+
![](/img/open-shell-compass.png)
26+
27+
### 5. Switch to the library database in the shell
28+
29+
Once the shell is loaded, execute: `use library` to ensure you are in the correct database.
30+
31+
![](/img/use-library-compass.png)
32+
33+
That's it for the setup, let's get our hands dirty!

docs/40-CRUD/1-WHERE.mdx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# 👐 WHERE → .find()
22

3-
Similar to SQL's `WHERE` clause, the `.find()` method in MongoDB retrieves documents from a collection that match a specified query.
3+
Similar to SQL's `WHERE` clause, the `.find()` method in MongoDB retrieves documents from a collection that matches a specified query.
44

55
## Syntax
66

77
```js
88
db.collection.find({ <query> })
99
```
1010

11-
- `<query>`: Specifies conditions to filter documents.
11+
- `<query>`: Specifies conditions to filter documents
1212

1313
## Example: Find all books from 2020
1414

1515
```js
1616
db.books.find({ year: 2020 });
1717
```
1818

19-
### Equivalent SQL Query
19+
### Equivalent SQL query
2020

2121
```sql
2222
SELECT * FROM books WHERE year = 2020;
@@ -26,24 +26,24 @@ SELECT * FROM books WHERE year = 2020;
2626

2727
The `find()` method takes a document as its first argument. This document specifies the filter criteria. You can use a variety of expressions within the filter document:
2828

29-
- **Comparison Operators:** `$eq` (equals), `$ne` (not equals), `$gt` (greater than), `$lt` (less than), `$gte` (greater than or equals), `$lte` (less than or equals), `$in` (in an array), `$nin` (not in an array).
30-
- **Logical Operators:** `$and`, `$or`, `$not`.
31-
- **Element Operators:** `$exists` (check for field existence), `$type` (check data type).
32-
- **Evaluation Operators:** `$regex` (regular expression matching), `$where` (JavaScript code execution).
33-
- **Geo-spatial Operators:** For location-based queries.
34-
- **Array Operators:** For querying arrays.
29+
- **Comparison operators:** `$eq` (equals), `$ne` (not equals), `$gt` (greater than), `$lt` (less than), `$gte` (greater than or equals), `$lte` (less than or equals), `$in` (in an array), `$nin` (not in an array)
30+
- **Logical operators:** `$and`, `$or`, `$not`
31+
- **Element operators:** `$exists` (check for field existence), `$type` (check data type)
32+
- **Evaluation operators:** `$regex` (regular expression matching), `$where` (JavaScript code execution)
33+
- **Geo-spatial operators:** For location-based queries
34+
- **Array operators:** For querying arrays
3535

3636
Now, let's utilize a few MongoDB operators and create more sophisticated queries:
3737

38-
### $or and $gt operator
38+
### $or and $gt operators
3939

40-
Suppose, we want to get all the books written in 2010 OR has more than 200 pages.
40+
Suppose we want to get all the books written in 2010 OR books that have more than 200 pages.
4141

4242
```sql
4343
SELECT * FROM books WHERE year = 2010 OR pages > 200
4444
```
4545

46-
Equivalent MongoDB Query:
46+
Equivalent MongoDB query:
4747

4848
```js
4949
db.books.find({
@@ -56,7 +56,7 @@ db.books.find({
5656

5757
### $and operator
5858

59-
This time, instead of OR let's query using AND:
59+
This time, instead of OR, let's query using AND:
6060

6161
```sql
6262
SELECT * FROM books WHERE year = 2010 AND pages > 200
@@ -75,17 +75,17 @@ db.books.find({
7575

7676
### Shorthand $and
7777

78-
When we are querying on 2 different fields and want to utilize $and, we can do so by passing a document with all the conditions like this:
78+
When we are querying on two different fields and want to utilize $and, we can do so by passing a document with all the conditions, like this:
7979

8080
```js
8181
db.books.find({ year: 2010, pages: { $gt: 200 } });
8282
```
8383

84-
As you can see, we don't have to pass an Array of conditions, MongoDB implicitly considers this as $and.
84+
As you can see, we don't have to pass an array of conditions. MongoDB implicitly considers this as $and.
8585

8686
## 👐 Challenge
8787

88-
Now, translate the following into a MongoDB Query.
88+
Now, translate the following into a MongoDB query.
8989

9090
#### 1. Find all books where `totalInventory` is exactly 5.
9191

@@ -109,13 +109,13 @@ Now, translate the following into a MongoDB Query.
109109
</div>
110110
</details>
111111

112-
#### 3. Find books in the "Science" genre that has more than 300 pages.
112+
#### 3. Find books in the "Science" genre that have more than 300 pages.
113113

114114
<details>
115115
<summary>Answer</summary>
116116
<div>
117117
```js
118-
db.books.find({ genre: "Science", pages: {$gt: 300} });
118+
db.books.find({ genres: "Science", pages: {$gt: 300} });
119119
```
120120
</div>
121121
</details>

docs/40-CRUD/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"label": "👐 CRUD operations",
2+
"label": "👐 CRUD Operations",
33
"position": 40,
44
"link": {
55
"type": "generated-index",

docs/50-aggregation/3-sort-limit.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ SELECT * FROM books ORDER BY timestamp DESC LIMIT 5;
7777
```js
7878
db.books.aggregate([
7979
{
80-
$match: { year: {$lt: 2010}}
80+
$match: { year: {$gt: 2010}}
8181
},
8282
{
8383
$project: {

docs/50-aggregation/4-group.mdx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,52 +91,61 @@ db.books.aggregate([
9191

9292
---
9393

94-
## 👐 Example 3: Find the average book rating of all books
94+
## 👐 Example 3: Find the total number of pages published every year.
95+
9596

9697
### **MongoDB query**
9798

9899
```js
99-
db.reviews.aggregate([
100-
{ $group: { _id: "$bookId", avgRating: { $avg: "$rating" } } },
100+
db.books.aggregate([
101+
{
102+
$group: {
103+
_id: "$year",
104+
totalPages: { $sum: "$pages" },
105+
},
106+
},
101107
]);
102108
```
103109

104110
### Equivalent SQL query
105111

106112
```sql
107-
SELECT bookId, AVG(rating) AS avgRating
108-
FROM reviews
109-
GROUP BY bookId;
113+
SELECT year, SUM(rating) AS totalPages
114+
FROM books
115+
GROUP BY year;
110116
```
111117

112118
### Sample output
113119

114120
```json
115121
[
116-
{ "_id": "1885865171", "avgRating": 4.2 },
117-
{ "_id": "0738701688", "avgRating": 4.33 },
118-
{ "_id": "0747545448", "avgRating": 5 }
122+
{ "_id": 1955, "totalPages": 664 },
123+
{ "_id": 1952, "totalPages": 416 },
124+
{ "_id": 1899, "totalPages": 128 }
125+
...
119126
]
120127
```
121128

122129
---
123130

124131
## 👐 Challenge
125132

126-
### 👐 1. Find the total number of pages published every year
133+
134+
### 👐 1. Find the average book rating of all books
127135

128136
<details>
129137
<summary>Answer</summary>
130138
```js
131-
db.books.aggregate([
139+
db.reviews.aggregate([
132140
{
133141
$group: {
134-
_id: "$year",
135-
totalPages: { $sum: "$pages" }
142+
_id: "$bookId",
143+
avgRating: { $avg: "$rating" }
136144
}
137145
},
138146
]);
139147
```
148+
140149
</details>
141150

142151
### 👐 2. Find users with the most number of reviews (Hint: use the `name` field in the reviews collection)

docs/50-aggregation/5-JOIN.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ db.authors.aggregate([
4747
]);
4848
```
4949

50-
### **Equivalent SQL Query**
50+
### **Equivalent SQL query**
5151

5252
```sql
53-
SELECT books.*, authors.*
53+
SELECT author.* , books_written.*
5454
FROM authors
55-
LEFT JOIN books ON authors._id = books.author_id;
55+
LEFT OUTER JOIN author_books ON authors.id = author_books.author_id
56+
LEFT OUTER JOIN books as books_written ON author_books.book_id = books_written._id;
5657
```
5758

5859
:::info
@@ -61,11 +62,11 @@ The result in MongoDB will have an array (`authorDetails`) instead of flat colum
6162

6263
---
6364

64-
## 🔹 Handling Unwinding ($unwind)
65+
## 🔹 Handling unwinding ($unwind)
6566

6667
Since `$lookup` produces an **array**, we can flatten it using `$unwind`.
6768

68-
### Example 2: Get Only Book Titles and Single Author Name
69+
### Example 2: Get only book titles and single author name
6970

7071
```js
7172
db.authors.aggregate([
@@ -83,7 +84,7 @@ db.authors.aggregate([
8384
```
8485

8586
:::info
86-
The $lookup operation creates an array within each book document. Using $unwind then flattens this array, resulting in a separate document for every single book - author pair.
87+
The $lookup operation creates an array within each book document. Using $unwind then flattens this array, resulting in a separate document for every single book-author pair.
8788
:::
8889

8990
---

docs/50-aggregation/7-CREATE-VIEW.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 👐 $merge
1+
# 🦸 $merge
22

33
In MongoDB, the **$merge** stage allows you to **write the results of an aggregation pipeline into a new or existing collection**. This is similar to the concept of "INSERT INTO ... SELECT" or "MERGE INTO" in SQL databases.
44

@@ -8,12 +8,12 @@ In MongoDB, the **$merge** stage allows you to **write the results of an aggrega
88

99
The `$merge` stage enables you to store aggregation results into a different collection. If the target collection doesn’t exist, MongoDB will create it automatically.
1010

11-
### **Key Features:**
11+
### **Key features:**
1212

13-
✔️ Inserts new documents if they don’t exist.
14-
✔️ Updates existing documents based on `_id` or a specified field.
15-
✔️ Can replace, merge, or discard duplicate records.
16-
✔️ Useful for **ETL workflows, reporting tables, and maintaining summary data.**
13+
✔️ Inserts new documents if they don’t exist
14+
✔️ Updates existing documents based on `_id` or a specified field
15+
✔️ Can replace, merge, or discard duplicate records
16+
✔️ Useful for **ETL workflows, reporting tables, and maintaining summary data**
1717

1818
---
1919

@@ -42,7 +42,7 @@ The `$merge` stage enables you to store aggregation results into a different col
4242

4343
---
4444

45-
## **🔹 Example 1: Creating a Summary Collection**
45+
## **🔹 Example 1: Creating a summary collection**
4646

4747
👉 Suppose we want to generate a collection that contains the **total number of books per genre**.
4848

@@ -61,7 +61,7 @@ db.books.aggregate([
6161
]);
6262
```
6363

64-
### **Equivalent SQL Query**
64+
### **Equivalent SQL query**
6565

6666
```sql
6767
INSERT INTO genre_summary (genre, totalBooks)
@@ -72,7 +72,7 @@ ON DUPLICATE KEY UPDATE totalBooks = VALUES(totalBooks);
7272

7373
---
7474

75-
## **🔹 Example 2: Maintaining an Author Summary Table**
75+
## **🔹 Example 2: Maintaining an author summary table**
7676

7777
👉 We want to create an **author_stats** collection with the total number of books written by each author.
7878

@@ -91,7 +91,7 @@ db.books.aggregate([
9191
]);
9292
```
9393

94-
### **SQL Equivalent**
94+
### **SQL equivalent**
9595

9696
```sql
9797
INSERT INTO author_stats (authorName, totalBooks)

docs/summary.mdx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ sidebar_position: 100
44

55
# 🎯 Summary
66

7-
Congratulations! Following this tutorial, you have successfully:
7+
Congratulations! Following this tutorial, you have successfully learned:
88

9-
- TBD
10-
- TBD
11-
- TBD
12-
- TBD
13-
- TBD
9+
- How to query collections in MongoDB
10+
- How to insert, update and delete documents in MongoDB
11+
- How to write aggregation pipelines in MongoDB
1412

1513
Visit the [MongoDB Developer Center](https://mongodb.com/developer/?utm_campaign=devrel&utm_source=workshop&utm_medium=cta&utm_content=sql_to_query_api&utm_term=sourabh_bagrecha) for more useful information and tutorials.

docusaurus.config.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,6 @@ const config = {
136136
hideable: true,
137137
},
138138
},
139-
announcementBar: {
140-
id: "feedback_form",
141-
content:
142-
'This is a demonstration that we can put a pop-up message here! Even <a target="_blank" rel="noopener noreferrer" href="#">links</a>',
143-
backgroundColor: "#fafbfc",
144-
textColor: "#091E42",
145-
isCloseable: true,
146-
},
147139
navbar: {
148140
title: `${title}`,
149141
logo: {

static/img/add-connection-compass.png

290 KB
Loading
341 KB
Loading

static/img/open-shell-compass.png

389 KB
Loading
385 KB
Loading

static/img/use-library-compass.png

106 KB
Loading

0 commit comments

Comments
 (0)