# schema

Canonical URL: https://developers.iagenify.com/sdk/ref/connected-storage/run/schema/
Language: en

`storage.run({ action: "schema", collection })`

The fields seen in a collection's recent documents, with their types. Enough to write a query without guessing.

Status: document database · free · sampled, not declared

A document database has no schema to read, so this builds one by looking: the twenty most recent documents, every field they carry, and the types it saw.

```javascript
await storage.run({ action: "schema", collection: "orders" });

// {
//   sampled: 20,
//   fields: [
//     { field: "_id",            types: ["objectId"], seenIn: 20 },
//     { field: "amount",         types: ["number"],   seenIn: 20 },
//     { field: "customer",       types: ["object"],   seenIn: 20 },
//     { field: "customer.email", types: ["string"],   seenIn: 19 },
//     { field: "refundedAt",     types: ["date", "null"], seenIn: 4 }
//   ]
// }
```

## Reading it properly {#reading}

| Signal | Means |
| --- | --- |
| seenIn equals sampled | Present on every document sampled. |
| seenIn well below sampled | Optional, or newly added. |
| Several types on one field | It is not consistent — handle both, or fix the data. |
| A field absent entirely | Not in the recent twenty. It may still exist on older ones. |

> **Twenty recent documents is a sample, not a contract**
>
> A field that stopped being written a year ago will not appear, and one added yesterday will dominate. It tells you how to query today's data; it does not describe everything in there.

## Nesting and size {#nesting}

Nested objects are walked two levels deep, dotted — `customer.email` — which is exactly how you filter on them. At most 150 fields come back.

```javascript
await storage.run({ action: "find", collection: "orders",
  filter: JSON.stringify({ "customer.email": "a@example.com" }) });
```

- [find](/sdk/ref/connected-storage/run/find): Lire les documents, une fois qu'on sait quoi demander.
- [run()](/sdk/ref/connected-storage/run): Toutes les actions, les garde-fous et ce que chacune coûte.

```python
storage.run(action="schema", collection="orders")

# {
#   "sampled": 20,
#   "fields": [
#     {"field": "_id",            "types": ["objectId"], "seenIn": 20},
#     {"field": "amount",         "types": ["number"],   "seenIn": 20},
#     {"field": "customer",       "types": ["object"],   "seenIn": 20},
#     {"field": "customer.email", "types": ["string"],   "seenIn": 19},
#     {"field": "refundedAt",     "types": ["date", "null"], "seenIn": 4}
#   ]
# }
```

```python
import json

storage.run(action="find", collection="orders",
            filter=json.dumps({"customer.email": "a@example.com"}))
```
