Embedded Documents
On this page
Use the Embedded Documents mapping rule to denormalize a foreign key relationship. With Embedded Documents you can nest child foreign key fields in a parent collection.
You have two main options when denormalizing your relational data with the Embedded Documents option:
You can embed the child documents into the parent and designate a Prefix and Field name.
You can merge into the parent, which represents the rows as fields at the parent level.
The Embedded Documents mapping rule is an advanced mapping rule option. It must be explicitly added to a mapping model.
About this Task
The Embedded Documents mapping rule is available if the relational table it is mapping from is on the one side of the foreign key relationship.
The table containing the foreign key must also be mapped to a collection in the MongoDB model.
Steps
(Optional) Configure multiple foreign keys
Allows you to select which foreign key to embed on when there is more than one foreign key between two tables. This option only appears for tables or collections with multiple foreign key links.
Select the name of the foreign key to embed on from the Foreign key link drop-down.
(Optional) Configure mapping rule filters
Allows you to explicitly include rows where an expression returns
true
. This differs from Table Filters,
which filter based on an SQL query, and apply to all rows from a
particular table. Mapping rule filters only apply to a specific
mapping.
On the Mappings pane, click the icon next to Advanced settings.
Select the Add mapping rule filter icon.
Enter a valid JavaScript filter expression in the Value expression text box.
Configure fields
Define the options for the new mapping rule. When defining the mapping rule options, you can:
Change the collection name.
To change the collection name, click the Name text box and enter the new name. Click outside of the text box to save the change. If the name is already in use, you receive a validation error.
Add Calculated Fields to create new fields based on relational source columns. See Calculated Fields for more information.
Change field names.
Include or exclude a field from your migration job by clicking the icon next to the field name.
Examples
This section demonstrates the relational input and the MongoDB output of the Embedded Documents mapping rule.
Embedded Documents
In this example:
A Embedded Documents mapping rule is applied to the
Order
table.The
OrderLine
table has two rows and theOrder
table has one row.The parent collection is set to OrderLine.
Merge fields into the parent is not selected.
The Field name is set to order.
Click the tabs below to see the input and output for this example:
OrderLine
table:
OrderLineID | OrderID | ProductID | Quantity |
---|---|---|---|
1 | 1 | 1 | 1 |
4 | 1 | 3 | 1 |
Order
table:
OrderID | CustomerID | TotalAmount | OrderStatusID |
---|---|---|---|
1 | 1 | 550.00 | 1 |
The documents in the OrderLine
collection now has the Order
details
embedded as an embedded document called Orders
.
{ ObjectID: { OrderLineID: 1, }, OrderID: 1, ProductID: 1, Quantity: 1, Orders: { OrderID: 1, CustomerID: 1, TotalAmount: 550, OrderStatusID: 1, }, }, { ObjectID: { OrderLineID: 4, }, OrderID: 1, ProductID: 3, Quantity: 1, Orders: { OrderID: 1, CustomerID: 1, TotalAmount: 550, OrderStatusID: 1, }, }
Merge fields into the parent
In this example:
The
OrderLine
table has four rows and the Order table has three rows.A Embedded Documents mapping rule is applied to the
Order
table.The parent collection is set to OrderLine.
Merge fields into the parent is selected.
The Prefix is set to (root).
Click the tabs below to see the input and output for this example:
OrderLine
table:
OrderLineID | OrderID | ProductID | Quantity |
---|---|---|---|
1 | 1 | 1 | 1 |
2 | 2 | 2 | 1 |
3 | 3 | 3 | 1 |
4 | 1 | 3 | 1 |
Order
table:
OrderID | CustomerID | TotalAmount | OrderStatusID |
---|---|---|---|
1 | 1 | 550.00 | 1 |
2 | 2 | 100.00 | 2 |
3 | 3 | 500.00 | 3 |
The documents in the OrderLine
collection have the Order
details
merged into the parent document as fields.
{ _id: { orderLineId: 1, }, orderId: 1, productId: 1, quantity: 1, customerId: 1, orderStatusId: 1, totalAmount: 550, }, { _id: { orderLineId: 2, }, orderId: 2, productId: 2, quantity: 1, customerId: 2, orderStatusId: 2, totalAmount: 100, }, { _id: { orderLineId: 3, }, orderId: 3, productId: 3, quantity: 1, customerId: 3, orderStatusId: 3, totalAmount: 500, }, { _id: { orderLineId: 4, }, orderId: 1, productId: 3, quantity: 1, customerId: 1, orderStatusId: 1, totalAmount: 550, }