Skip to content

Commit 94ad8e9

Browse files
committed
Updated mapping rules to match new relational schema
1 parent d5d6c69 commit 94ad8e9

14 files changed

+135
-92
lines changed

docs/60-project-settings.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Screenshot from "@site/src/components/Screenshot";
77
- Select single inherited primary key. This detects the primary key from the relational schema and sets them as the `_id` in MongoDB.
88
- Optionally, toggle Suggested Mappings on and off.
99

10+
If we don't do this we'll have both an autogenerated `_id` field of type `ObjectId` and a `id` field copied from the relational DB.
1011

1112
<Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/90-image-001.png" alt="Screenshot of the connect modal" />
1213

docs/80-edit-mapping-rules/10-edit-mapping-books-book-attribute.mdx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ We want to clean up the book_attributes array in our MongoDB schema. We want an
2222
}
2323
```
2424

25-
- On the MongoDB diagram view, click the `books` collection
25+
- On the MongoDB diagram view, click on the `books` collection.
2626
- On the relational mappings list on the right, click on the edit icon of `book_attribute` embedded array mapping rule.
27-
- Change the Field name from `bookAttributes` to `attributes`
28-
- Uncheck `bookId`. We just want all the attributes, which are key/value pairs.
29-
- Click `Save and close`
27+
- Change the Field name from `bookAttributes` to `attributes`.
28+
- Uncheck `bookId`. We just want all the attributes, which are key/value pairs. Also, as we're embedded inside books, we don't need the `bookId` anymore.
29+
- Click `Save and close`.
3030

3131
:::info
3232
Here we've implemented the [Attribute Pattern](https://www.mongodb.com/developer/products/mongodb/attribute-pattern/) for MongoDB. This will allow for easy indexing, querying based on attributes and extension of attributes.
3333
:::
3434

35-
<Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/150-image-001.png" alt="Screenshot of the connect modal" />
36-
35+
<Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/150-image-001.png" alt="Screenshot of the connect modal" />

docs/80-edit-mapping-rules/100-edit-mapping-issue-detail-issue-detail.mdx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import Screenshot from "@site/src/components/Screenshot";
22

3-
# 🦸 Edit mapping to issueDetails collection (from issue_details table)
3+
# 🦸 Remove unnecessary ids from issueDetails
44

55
:::info
66
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.
77
:::
88

9-
Now we want to remove some unncessary id fields from the issueDetails collection
9+
Now we want to remove some unncessary id fields from the issueDetails collection. We have `bookId` and `userId` but we also have these values in the embedded `book` and `user` fields.
1010

11-
12-
- On the MongoDB diagram view, click the `issueDetails` collection
13-
- On the relational mappings list on the right, click on the edit icon of `issue_details` New Documents mapping rule
11+
- On the MongoDB diagram view, click the `issueDetails` collection.
12+
- On the relational mappings list on the right, click on the edit icon of `issue_details` New Documents mapping rule.
1413
- Uncheck the `bookId` and `userId` fields, and leave all other fields checked.
15-
- Click `Save and close`
14+
- Click `Save and close`.
1615

1716

1817

docs/80-edit-mapping-rules/110-add-mapping-reviews-user.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import Screenshot from "@site/src/components/Screenshot";
22

3-
# 🦸 Add mapping to reviews collection (from users table)
3+
# 🦸 Add user's names to reviews
44

55
:::info
66
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.
77
:::
88

99
We want to store each user's name against their reviews.
1010

11-
- On the MongoDB diagram view, click the `reviews` collection
12-
- On the relational mappings list on the right, click `+Add`
13-
- Select `Embedded documents`
14-
- Select `users` as Source table
15-
- In the advanced settings check `Merge fields into the parent`
16-
- Only check `name` field
17-
- Click `Save and close `
11+
- On the MongoDB diagram view, click the `reviews` collection.
12+
- On the relational mappings list on the right, click `+Add`.
13+
- Select `Embedded documents`.
14+
- Select `users` as Source table.
15+
- In the advanced settings check `Merge fields into the parent`.
16+
- Only check `name` field.
17+
- Click `Save and close`.
1818

1919

2020
<Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/200-image-001.png" alt="Screenshot of the connect modal" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Screenshot from "@site/src/components/Screenshot";
2+
3+
# 🦸 Clean Users address data
4+
5+
:::info
6+
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.
7+
:::
8+
9+
The array that holds user addresses has a lot of unneeded data.
10+
11+
- On the MongoDB diagram view, click on the `users` collection
12+
- Click on the edit edit icon of `userAddresses`
13+
- Change the field name from `userAddresses` to `addresses`.
14+
- Deselect `id`, `userId` and `cityId`.
15+
- Click `Save and close `
16+
- Click on the edit edit icon of `cities`, which is an embedded document inside the array `addresses`.
17+
- Deselect `id`.
18+
- Rename the `name` field into `cityName`.
19+
- In the advanced settings, check 'Merge fields into parent`.
20+
- Click `Save and close`.
21+
- On the relational mappings list on the right, click `+Add`.
22+
- Select `Embedded documents`.
23+
- Select `countries` as Source table.
24+
- In the advanced settings check `Merge fields into the parent`.
25+
- Only check `name` field, renaming the `name` field into `countryName`.
26+
- Click `Save and close`.
27+
28+
29+
![Screenshot of the users collection](/img/users.png)
30+

docs/80-edit-mapping-rules/20-edit-mapping-books-book-genre.mdx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,32 @@ We'll go from the proposed array of objects:
99
```json
1010
bookGenres: [
1111
{
12-
bookId: "Some Book Id",
13-
genres: "fiction"
12+
"bookId": "Some Book Id",
13+
"genres": "fiction"
1414
},
1515
{
16-
bookId: "Some Book Id",
17-
genres: "novel"
16+
"bookId": "Some Book Id",
17+
"genres": "novel"
1818
}
1919
]
2020
```
2121

22-
to an array of primitive values
22+
to an array of
2323

2424
```json
25-
genres: [ "fiction", "novel" ]
25+
genres: [
26+
{ "name": "fiction" },
27+
{ "name" :"novel" }
28+
]
2629
```
2730

28-
- On the MongoDB diagram view, click the `books` collection
29-
- On the relational mappings list on the right, click on the edit icon of `book_genre` embedded array
30-
- Change the field name from `bookGenres` to `genres`
31-
- Uncheck `bookId`
32-
- In the advanced settings, check `create array of primitive values`
33-
- Click `Save and close`
31+
- On the MongoDB diagram view, click the `books` collection.
32+
- Click on the edit edit icon of `genres`. Deselect `id`, This is a child relationship of `bookGenres`, and we'll merge these fields into the parent, to avoid too much unneccesary nesting.
33+
- Click `Save and close`.
34+
- Click on the edit icon of the `book_genre` embedded array.
35+
- Change the field name from `bookGenres` to `genres`.
36+
- Uncheck `bookId` and `genreId`.
37+
- Click `Save and close`.
3438

3539

3640

docs/80-edit-mapping-rules/30-add-mapping-books-authors.mdx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,47 @@ import Screenshot from "@site/src/components/Screenshot";
22

33
# 👐 Add authors details to the books collection
44

5-
We want to modify the MongoDB schema so that each book contains an array of its authors.
6-
In this step we will embed the book details into the empty array created from the join table (which right now only contains `authorId` and `bookId`)
5+
## 👐 Add author name to the books collection
6+
7+
We want to modify the MongoDB schema so that each book contains an array of its authors. Right now in the `authorBooks` mapping we're getting the `authorId` for the current book, but we want also the name of the author. So in this step we will embed the author name into array created from the mapping of the join table (which right now only contains `authorId` and `bookId`)
78

89
- On the MongoDB diagram view, click the `books` collection
910
- On the relational mappings list on the right, click `+ Add`
1011
- Select `Embedded documents`
1112
- Select `authors` as Source table
1213
- In the advanced settings, check 'Merge fields into parent`
13-
- Check the `id` and `name` fields and uncheck the rest
14+
- Check the `name` field and uncheck the rest
1415
- Click `Save and close`
1516

16-
:::info
17-
This is a perfect example of the [Extended Reference Pattern](https://www.mongodb.com/blog/post/building-with-patterns-the-extended-reference-pattern). When we read a book, we have some of the author's data (`name` in this case) but we still have the author's id in case we need all the author's attributes (we can then do a join on read using `$lookup`)
18-
:::
1917

2018
<Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/130-image-001.png" alt="Screenshot of the connect modal" />
2119

20+
## 👐 Remove unneeded bookId from author details in books collection
21+
22+
We want to modify the MongoDB schema so that each book contains an array of its authors.
23+
In this step we will remove the redundant ID fields from the join table.
24+
25+
- On the MongoDB diagram view, click the `books` collection.
26+
- On the relational mappings list on the right, click on the edit icon of `author_book`.
27+
- Edit the field name from `authorBooks` to just `authors`.
28+
- In the list of fields, uncheck `bookId`.
29+
- Click `Save and close`.
30+
31+
This results in our `authors` array looking like:
32+
33+
```json
34+
"authors": [
35+
{
36+
"authorId": 8282,
37+
"name": "Cervantes"
38+
},
39+
...
40+
]
41+
```
42+
43+
<Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/140-image-001.png" alt="Screenshot of the connect modal" />
44+
45+
46+
:::info
47+
This is a perfect example of the [Extended Reference Pattern](https://www.mongodb.com/blog/post/building-with-patterns-the-extended-reference-pattern). When we read a book, we have some of the author's data (`name` in this case) but we still have the author's id in case we need all the author's attributes (we can then do a join on read using [`$lookup`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/))
48+
:::

docs/80-edit-mapping-rules/40-edit-mapping-books-author-book.mdx

Lines changed: 0 additions & 17 deletions
This file was deleted.

docs/80-edit-mapping-rules/50-add-mapping-books-reviews.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ This is an implementation of the [Subset Pattern](https://www.mongodb.com/blog/p
1010

1111
In this step we will embed the book details into the empty array from the join table.
1212

13-
- On the MongoDB diagram view, click the `books` collection
14-
- On the relational mappings list on the right, click `+ Add`
15-
- Select `Embedded array`
16-
- Select `reviews` as Source table
17-
- In the advanced settings, check `Add array conditions`
18-
- In `Sort by and order` option, select `timestamp` and toggle the order to descending
19-
- Set `Limit number of rows` to `3`
20-
- Uncheck the `bookId` and leave the rest of the fields checked
21-
- Click `Save and close`
13+
- On the MongoDB diagram view, click the `books` collection.
14+
- On the relational mappings list on the right, click `+ Add`.
15+
- Select `Embedded array`.
16+
- Select `reviews` as Source table.
17+
- In the advanced settings, check `Add array conditions`.
18+
- In `Sort by and order` option, select `timestamp` and toggle the order to descending.
19+
- Set `Limit number of rows` to `3`.
20+
- Uncheck the `bookId` and leave the rest of the fields checked.
21+
- Click `Save and close`.
2222

2323
Limitations:
2424
- Continuous Migration Mode will not support this transformation
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import Screenshot from "@site/src/components/Screenshot";
22

3-
# 👐 Add aliases to authors
3+
# 👐 Edit author's aliases array
44

55
We want to modify the MongoDB schema so that each author's aliases are represented as an array of strings.
66

7-
- On the MongoDB diagram view, click the `authors` collection
8-
- On the relational mappings list on the right, click on the edit icon of `author_alias` embedded array mapping rule
9-
- In the list of fields, uncheck `authorId`
10-
- In the mapping rule's advanced settings, Check `create array of primitive values`
11-
- Click `Save and close`
7+
- On the MongoDB diagram view, click on the `authors` collection.
8+
- On the relational mappings list on the right, click on the edit icon of `author_alias` embedded array mapping rule.
9+
- In the list of fields, uncheck `authorId`.
10+
- In the mapping rule's advanced settings, Check `create array of primitive values`.
11+
- Click `Save and close`.
1212

1313
<Screenshot url="https://www.mongodb.com/products/tools/relational-migrator" src="img/110-image-001.png" alt="Screenshot of the connect modal" />
1414

0 commit comments

Comments
 (0)