---
title: Document Model
slug: v4-5/document-model
docTags: 
createdAt: 2023-08-13T16:07:17.305Z
---

Data is stored in Ditto as a schema-flexible unit referred to as a *document.&#x20;*&#x54;he data that documents contain can be exposed using a variety of data formats; however, JSON format is most common.

A set of documents grouped together is referred to as a *collection*. A collection can be thought of like a *table* in a relational database *table&#x20;*— but with greater flexibility in terms of the data it can hold — and the documents in a collection like table *rows.*

The following table provides a basic definition of a document; the fundamental unit of data storage in Ditto:

:::hint{type="info"}
For more information, see *Ditto Basics&#x20;*> [Data Structures and Types](docId\:P6OPuBbnA--hc2DCjWshS).
:::

| **Structure**      | A Ditto document is a group of key-value pairs:                                                                                                                                                                                                 |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Content**        | - The values in a document can include various scalar types like `strings`, `numbers`, `booleans`, `arrays`, and more.
- Documents can contain nested `MAP` data types, allowing them to represent complex hierarchical structures.             |
| **Identification** | Each document has a unique `_id` field that acts at a primary key. <br />The `_id` field is automatically added by Ditto if not provided during document creation.                                                                              |
| **Flexibility**    | Ditto is schema-less, meaning that documents within the same collection do not need to have the same set of fields or structure. This flexibility allows for the evolution of data models over time without requiring changes to existing data. |

***

# Fields

A document consists of sets of fields that self-describe the data it encodes. Each set signifies a single pair of two associated elements:

- The name identifying the field.&#x20;
- The value that holds the data.&#x20;

:::CodeblockTabs
Ditto Document

```json
{
  _id: "123",
  field1: "abc",
  field2: 1,
  field3: {
    sub_field1: "xyz",
    sub_field2: 2
  }
}
```
:::

Similar to most document-oriented databases, you can only use `strings` to encode field names in documents.&#x20;

For complete naming rules, see [Ditto Query Language](docId\:e3UQqeqVzvOTjlS2xfDFl) > [IDs, Paths, Strings, and Keywords](docId\:obsL86Zmxluex0UhjFcPh) > [Identifier Rules](docId\:obsL86Zmxluex0UhjFcPh).

:::hint{type="warning"}
If you attempt to insert a type other than a `string` for a field property, as demonstrated in the following snippet, you will receive an error message:
:::

:::CodeblockTabs
&#x20;

```none
{
    1: "numeric_fields_are_invalid_here",
    "this_part": "numeric_fields_are_valid_here"
}
```
:::

## Nested Fields

In the following structure, the `location` field property is logically grouped with details about the car, such as its `make`, `year`, and `color`.&#x20;

It contains nested fields. also referred to as *subfields*, representing both the `coordinates` and the `address` where the car is located:

::Image[]{src="https://api.archbee.com/api/optimize/qoRkNxW5fJ81r_NqVpc8C/nE76-8SxgOLCjFhcWaVqc_embedded-object.png" size="72" width="2259" height="1265" position="flex-start" showCaption="false"}

***

# Values

Values can be encoded using various data types, including scalar type, providing flexibility in representing a wide range of information.

Data types provide flexibility in representing a range of information and consist of a merge behavior plus a scalar value. Data types include `REGISTER`, `MAP`, and `ATTACHMENT`

For more information and how-to instructions, see *Platform Manual&#x20;*> [Data Types](docId\:VoFl0GukXq8AjsMMW7Sgz).
