You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/50-aggregation/5-JOIN.mdx
+12-3Lines changed: 12 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -47,17 +47,26 @@ db.authors.aggregate([
47
47
]);
48
48
```
49
49
50
+
### **Equivalent SQL query**
51
+
52
+
```sql
53
+
SELECT author.* , books_written.*
54
+
FROM authors
55
+
LEFT OUTER JOIN author_books ONauthors.id=author_books.author_id
56
+
LEFT OUTER JOIN books as books_written ONauthor_books.book_id=books_written._id;
57
+
```
58
+
50
59
:::info
51
60
The result in MongoDB will have an array (`authorDetails`) instead of flat columns.
52
61
:::
53
62
54
63
---
55
64
56
-
## 🔹 Handling Unwinding ($unwind)
65
+
## 🔹 Handling unwinding ($unwind)
57
66
58
67
Since `$lookup` produces an **array**, we can flatten it using `$unwind`.
59
68
60
-
### Example 2: Get Only Book Titles and Single Author Name
69
+
### Example 2: Get only book titles and single author name
61
70
62
71
```js
63
72
db.authors.aggregate([
@@ -75,7 +84,7 @@ db.authors.aggregate([
75
84
```
76
85
77
86
:::info
78
-
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.
0 commit comments