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/60-lookups/2-advanced-lookups.mdx
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
# 🦸♂️ Advanced lookups
2
2
3
3
:::info
4
-
Extra activity, do it if you have extra time or are following at home, won't be covered during the hands-on Lab
4
+
Extra activity! Do it if you have extra time or are following along at home. It won't be covered during the hands-on lab.
5
5
:::
6
6
7
7
We get this request: Write a `$lookup` to get `name` and `bio` from author's information inside each book document. To get this done, we need to review several things:
8
8
9
-
-each book can have several authors. This many to many relationship (as an author can also write many books) is modelled using two different arrays: a `books` array in the `authors` collection and an `authors` array in the `books` collection.
10
-
-so we'll need to get a separate document for each book that has more than one author. If a book has three authors we'll use `$unwind` to get three documents with the same data except for the author, that will be each of the three authors.
9
+
-Each book can have several authors. This many-to-many relationship (as an author can also write many books) is modeled using two different arrays: a `books` array in the `authors` collection and an `authors` array in the `books` collection.
10
+
-So we'll need to get a separate document for each book that has more than one author. If a book has three authors, we'll use `$unwind` to get three documents with the same data except for the author, which will be each of the three authors.
11
11
12
-
You can try this with this Aggregation Pipeline:
12
+
You can try this with this aggregation pipeline:
13
13
14
14
```js
15
15
db.books.aggregate([
@@ -19,7 +19,7 @@ db.books.aggregate([
19
19
{$project: {attributes:0, reviews:0}}
20
20
])
21
21
```
22
-
-now, we need to get the authors'information. For that, we'll use `$lookup`, linking the `_id` in the `authors` collection with the `_id` we have in each book's `authors` array. But as we can see here, these have a different type: the ones inside our array are Strings, while the `author` collection `_id` are `ObjectId`.
22
+
-Now, we need to get the authors'information. For that, we'll use `$lookup`, linking the `_id` in the `authors` collection with the `_id` we have in each book's `authors` array. But as we can see here, these have a different type: The ones inside our array are strings, while the `author` collection `_id` are `ObjectId`.
23
23
24
24
```js
25
25
authors: {
@@ -28,7 +28,7 @@ db.books.aggregate([
28
28
},
29
29
```
30
30
31
-
So we need to convert from `String` into `ObjectId`. We can do that using `$toObjectId`. This will add a new field `authorId` converting it into `ObjectId`:
31
+
So we need to convert from `String` into `ObjectId`. We can do that using `$toObjectId`. This will add a new field,`authorId`, converting it into `ObjectId`:
32
32
33
33
```js
34
34
db.books.aggregate([
@@ -41,7 +41,7 @@ db.books.aggregate([
41
41
])
42
42
```
43
43
44
-
-now we're ready to do the `$lookup`: we want all documents from `authors` that have the same `_id` as the `authorId` we just created. We use a `pipeline` to get just `authors``name` and `bio`.
44
+
-Now, we're ready to do the `$lookup`: We want all documents from `authors` that have the same `_id` as the `authorId` we just created. We use a `pipeline` to get just `authors``name` and `bio`.
0 commit comments