# Document Store

Document Store provides set of functionalities to store and retrieve JSON, Arrays, Objects, and Collection of Objects in a way that you are free from knowledge about storage and serialization and deserialization.

* [DBAL Support](https://docs.ecotone.tech/modules/dbal-support#document-store)

## How to use

If you have installed supporting module like DBAL, then it's automatically registered in your `Depedency Container`.

```php
final class UserStore
{
    public function __construct(private DocumentStore $documentStore) {}

    public function store(User $user): void
    {
        $this->documentStore->addDocument("users", $user->getId(), $user);
    }
    
    public function getUser(int $userId): User
    {
        return $this->documentStore->getDocument("users", $userId);
    }
}
```

The first parameter, which in above example is `"users"` is collection under which users will be stored. You may use different collections for different objects to separate them.\
\
In `store method` we are passing `User` object to `addDocument` Ecotone will convert this object to `JSON` and store it chosen provider's storage.\
\
When we fetch the user using `getDocument` we are passing the saved collection name and user id, the conversion back to the user will be done automatically.

### Storing Aggregates in your Document Store

With Dbal Document Store you can [enable State-Stored Aggregate Repository backed by Document Store](https://docs.ecotone.tech/modules/dbal-support#standard-aggregate-repository).

## Conversions

You may store JSON or simple arrays, objects and collection of objects (User\[]).

{% hint style="info" %}
You will need to have Converter for JSON registered.\
If you are using `ecotone/jms-converter`, then it will be done for you.
{% endhint %}

## Other Possibles Methods

Document Store provides set of methods, like `drop whole collection`, `updating / upserting document`, `deleting` and `counting documents.`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ecotone.tech/messaging/document-store.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
