Skip to content

Commit 6488214

Browse files
authored
Merge pull request #28 from Meg528/patch-20
Update 2-advanced-lookups.mdx
2 parents fce1bbd + 1b418f9 commit 6488214

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

docs/60-lookups/2-advanced-lookups.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# 🦸‍♂️ Advanced lookups
22

33
:::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.
55
:::
66

77
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:
88

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.
1111

12-
You can try this with this Aggregation Pipeline:
12+
You can try this with this aggregation pipeline:
1313

1414
```js
1515
db.books.aggregate([
@@ -19,7 +19,7 @@ db.books.aggregate([
1919
{$project: {attributes: 0, reviews: 0}}
2020
])
2121
```
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`.
2323

2424
```js
2525
authors: {
@@ -28,7 +28,7 @@ db.books.aggregate([
2828
},
2929
```
3030

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`:
3232

3333
```js
3434
db.books.aggregate([
@@ -41,7 +41,7 @@ db.books.aggregate([
4141
])
4242
```
4343

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`.
4545

4646
```js
4747
db.books.aggregate([

0 commit comments

Comments
 (0)