---
title: Nested Hierarchy
slug: v4-5/document-model/complex-structures/nested-hierarchy
docTags: 
createdAt: 2023-11-16T17:41:00.608Z
---

If you need to represent a highly complex dataset in a document, you can embed a `MAP` data type within the document.

:::hint{type="warning"}
Avoid using `arrays` in Ditto.&#x20;

Due to potential merge conflicts when offline peers reconnect to the mesh and attempt to sync their updates, especially when multiple peers make concurrent updates to the same item within the `array`.
:::

# Embedding Map Structures

Embedding a `MAP` provides a way for you to structure and organize related data within a single document to create a complex structure with multiple levels of hierarchy. As in, you can embed a `MAP` within a `MAP`, within another `MAP`, within another `MAP`, and so on.&#x20;

:::hint{type="info"}
You can model relationships between your data using foreign-key and key-value relationships by way of embedded `MAP`. For more information, see [Relationships](docId:45G2fwjGTrn8y9Ne7W86j).
:::

For example, the following snippet shows three levels of embedded `maps`: `details`, `engine`, `interior`, and `features`.&#x20;

```json
{
  "_id": {
    "vin": "123abc",
    "make": "Toyota",
    "model": "Corolla",
    "year": 2022,
  },
  "details": {
    "engine": {
      "type": "Gasoline",
      "displacement": "1.8L"
    },
    "interior": {
      "seats": 5,
      "color": "Black"
    },
    "features": {
      "safety": {
        "airbags": 6,
        "antilockBrakes": true
      },
      "technology": {
        "infotainment": "Touchscreen",
        "navigation": true
      }
    }
  }
}

```

Each level contains its own key-value pairs and, if used, children-level `MAP`. You can represent key values using a `REGISTER`, `ATTACHMENT` or another `MAP`. For more information, see [Data Types](docId\:VoFl0GukXq8AjsMMW7Sgz).

***

# Benefits of Embedding Maps

Embedding a `MAP` is beneficial in scenarios where you need to manage a collection of items and continuously modify that collection over time; that is you want to link multiple data items with a single unique `string` identifier, but you anticipate that these data items are subject to concurrent edits over time.&#x20;

As an example, the following snippet demonstrates a basic Point-of-Sale (PoS) system where you need to keep track of the customer `orders` collection. And, since multiple users can add and remove orders within the collection, you embed a map to represent the ordered items, where each key denotes an item ID and the linked value indicates the quantity ordered:

:::CodeblockTabs
pseudocode

```javascript
const order = {
  customerName: 'John Doe',
  orderDate: '2023-08-15',
  items: {
    'item123': 2, // Item ID: Quantity ordered
    'item456': 5,
    'item789': 1
  }
};

// Inserting the order into the Ditto collection
await ditto.store.execute(`
  UPDATE INTO orders
  DOCUMENTS (:order)`,
  { order });
```
:::

