---
title: Collections
slug: v4-5/document-model/collections
docTags: 
createdAt: 2023-08-13T16:32:45.845Z
---

Ditto syncs and queries documents through a combination of *collection* names and document identifiers (`_id`). A collection is a grouping of documents, like *tables* in a relational database system but far simpler and more flexible.&#x20;

::Image[]{src="https://api.archbee.com/api/optimize/qoRkNxW5fJ81r_NqVpc8C/fcqm-eUpUunzxjkGDX_4y_document-demo-table.png" size="74" width="2836" height="1540" position="flex-start" showCaption="false"}

# Creating a Collection&#x20;

A document must always belong to a collection, even if only one document is associated with that collection name.&#x20;

There's no explicit step to creating a collection; when you create a document with a specified collection name, Ditto automatically handles the referencing of that collection. That is, the collection name is assigned implicitly by the documents themselves. When you save a document and associate it with a particular collection name (like `"cars"` or `"boats"`), Ditto internally manages the collection. If the specified collection name doesn't exist yet, Ditto creates it as soon as the first document is associated with that name.

:::hint{type="info"}
For more information on creating documents, see, see [CRUD Operations](docId\:LBRSIPPkeZdqTGBM4i5NN) > [CREATE](docId\:QsOasGYrr0l0DYdXy77cA).
:::

# Collections Modeling

While it is typically common for all documents in a collection to have the same structure, it is not a technical requirement.&#x20;

For example, all documents referencing cars can go in the `"cars"` collection, and boat documents in the `"boats"` collection. You can create any number of collections that best represent your data model.

# Querying a Collection&#x20;

:::::VerticalSplit{layout="middle"}
:::VerticalSplitItem
Ditto queries against collections, *not* documents.&#x20;

For example, querying an entire `cars` collection:
:::

::::VerticalSplitItem
:::CodeblockTabs
DQL

```sql
SELECT * FROM cars
```
:::
::::
:::::

## Querying a Collection With Non-Register Data Types

When querying a collection that contains data types other than `REGISTER` — so when you're querying for a `MAP` or `ATTACHMENT` type — it is crucial to declare their types. Failing to do so will leave Ditto unsure about where to search.

To declare their types, prefix the collection name with the `COLLECTION` keyword followed by a *type definition*. A type definition expresses the data types for the fields.&#x20;

For example, here querying is against the `cars` collection with a field `properties` of data type `MAP`:

:::CodeblockTabs
DQL

```sql
SELECT * FROM COLLECTION cars (properties MAP)
```
:::

For complete DQL syntax, see *Ditto Query Language&#x20;*> [Types and Definitions](docId\:GsuSiC4zSrjq_0h07Ckn_)

***

# System Collections

Double underscore (\_\_) denotes a Ditto system collection and is a reserved identifier prefix for collections.&#x20;

***

# Collection Naming

When naming a collection, make sure to adhere to the DQL identifier rules provided in the *DQL Reference Guide* > [IDs, Paths, Strings, and Keywords](docId\:obsL86Zmxluex0UhjFcPh) > [Identifier Rules](docId\:obsL86Zmxluex0UhjFcPh).
