> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Schema Mapping

Schema mapping is the process by which you decide how your source relational schema should be represented in your target MongoDB database. Facilitate the schema mapping process in Relational Migrator through customized mappings rules.

## Mapping Rules

During the schema mapping design process, Relational Migrator automatically derives the source data model from the schema of your relational database. The destination data model can be influenced by customizing:

- Mapping Rule options

- Project ID field options

![Schema Mapping Concept](/img/schema-mapping/schema-mapping-relational-migrator-concept-diagram-b.png)

**Tip:**

Relational Migrator supports Oracle materialized views. If your data model requires complex mappings that involve many tables, you can create a materialized view using a custom SQL query instead. The materialized view acts as a single source table and simplifies the required mappings. Performing complex joins in your Oracle source database can streamline your migration project and may improve performance compared to a migration job that performs the same transformations live. For more information, see the [Oracle SQL Language Reference.](https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/CREATE-MATERIALIZED-VIEW.html)

Relational Migrator does not support materialized views for other databases at this time.

## Example

This section provides a scenario and example implementation for the schema mapping process. This example converts a relational data model to a MongoDB data model.

Using the `MongoEnterprises` relational database, the goal is to flatten all of the database's tables, so that all the order and customer data is available in a single MongoDB collection.

### Use-Case Denormalizing Table Structure

The following image shows the relational data model to be denormalized using the Relational Migrator.

![Relational Data Model](/img/schema-mapping/schema-mapping-mongo-enterprises.png)

### Mapping Rule Results

When using Relational Migrator, the goal is to achieve the following MongoDB data model. The `Order` collection contains all the child elements from the `Customer`, `Order`, `Product` and `Order Line` tables. The collection has nested objects for the migrated tables. The results look like this:

```json
{
  "_id": {
    "OrderID": 1
  },
  "CustomerID": 1,
  "OrderStatusID": 1,
  "TotalAmount": 550,
  "Customer": {
    "CustomerID": 1,
    "Name": "Joelynn Fawthrop",
    "Address1": "86 Dwight Pass",
    "Address2": "Carregal",
    "Address3": "3800-854"
  },
  "OrderLines": [
    {
      "OrderLineID": 1,
      "OrderID": 1,
      "ProductID": 1,
      "Quantity": 1,
      "Product": {
        "ProductID": 1,
        "Name": "MongoDB 5.0 Action Figure",
        "Price": 50
      }
    },
    {
      "OrderLineID": 4,
      "OrderID": 1,
      "ProductID": 3,
      "Quantity": 1,
      "Product": {
        "ProductID": 3,
        "Name": "Gold Plated MongoDB Compass",
        "Price": 500
      }
    }
  ],
  "OrderStatus": {
    "OrderStatusID": 1,
    "Name": "Order Placed"
  }
}
```

### Mapping Rule Implementation

To accomplish the target data model results with Relational Migrator, configure the `Order` table with the following mapping rule options:

| Relational Table | Mapping Rule Type | Root Path |
| --- | --- | --- |
| Order | [New Documents](https://www.mongodb.com/docs/relational-migrator/mapping-rules/mapping-rule-options/new-documents.md#std-label-rm-new-documents) | N/A |
| OrderLine | [Embedded array](https://www.mongodb.com/docs/relational-migrator/mapping-rules/mapping-rule-options/embedded-array.md#std-label-rm-embedded-array) | `OrderLines` |
| Customer | [Embedded documents](https://www.mongodb.com/docs/relational-migrator/mapping-rules/mapping-rule-options/embedded-documents.md#std-label-rm-embedded-documents) | `Customer` |
| OrderStatus | [Embedded documents](https://www.mongodb.com/docs/relational-migrator/mapping-rules/mapping-rule-options/embedded-documents.md#std-label-rm-embedded-documents) | `OrderStatus` |
| Product | [Embedded documents](https://www.mongodb.com/docs/relational-migrator/mapping-rules/mapping-rule-options/embedded-documents.md#std-label-rm-embedded-documents) | `OrderLInes.Product` |
