# find

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

`storage.run({ action: "find", collection, filter, options })`

Documents matching a filter, with paging, sorting and a choice of fields.

Status: document database · free · filter as JSON text

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `collection` | string | required | Which collection. |
| `filter` | JSON text |  | The query. Omitted or empty means everything — bound it with `limit`. |
| `options.limit` | integer |  | How many. `0` means no limit, which is rarely what you want. |
| `options.skip` | integer |  | How many to step over — this plus `limit` is paging. |
| `options.sort` | object |  | `{ field: 1 \| -1 }`. |
| `options.projection` | object |  | Which fields to return. Fewer fields, smaller answer. |

```javascript
await storage.run({
  action: "find",
  collection: "orders",
  filter: JSON.stringify({ status: "paid", amount: { $gt: 100 } }),
  options: { limit: 20, skip: 0, sort: { createdAt: -1 },
             projection: { _id: 1, amount: 1, customer: 1 } },
});
// [{ _id: "…", amount: 240, customer: { … } }]
```

> **Send the filter as JSON text**
>
> Not as an object. A nested object loses keys beginning with $ on the way through some callers, and the query then matches more than you meant, without saying so.

## Identifiers are converted for you {#ids}

An `_id` given as a string is turned into the identifier type the database expects, including inside `$in`, `$nin` and `$ne`. So you can pass back an id you printed, without converting it yourself.

```javascript
filter: JSON.stringify({ _id: "66f0c1a2b3c4d5e6f7a8b9c0" })
filter: JSON.stringify({ _id: { $in: [idA, idB] } })
```

## Paging {#paging}

```javascript
async function* pages(storage, collection, filter, size = 100) {
  for (let skip = 0; ; skip += size) {
    const rows = await storage.run({ action: "find", collection, filter,
      options: { limit: size, skip, sort: { _id: 1 } } });
    if (!rows.length) return;
    yield rows;
    if (rows.length < size) return;
  }
}
```

Sort on something stable — `_id` is the safe default. Paging an unsorted query can show you the same document twice and miss another.

## What is refused {#refused}

A filter containing `$where`, `$function` or `$accumulator` is refused: they run code inside the database. The refusal happens before the query is sent.

- [count](/sdk/ref/connected-storage/run/count): Combien, sans les lire.
- [aggregate](/sdk/ref/connected-storage/run/aggregate): Grouper et calculer plutôt que ramener.
- [run()](/sdk/ref/connected-storage/run): Toutes les actions, les garde-fous et ce que chacune coûte.

```python
import json

storage.run(
    action="find",
    collection="orders",
    filter=json.dumps({"status": "paid", "amount": {"$gt": 100}}),
    options={"limit": 20, "skip": 0, "sort": {"createdAt": -1},
             "projection": {"_id": 1, "amount": 1, "customer": 1}},
)
# [{"_id": "…", "amount": 240, "customer": {…}}]
```

```python
filter = json.dumps({"_id": "66f0c1a2b3c4d5e6f7a8b9c0"})
filter = json.dumps({"_id": {"$in": [id_a, id_b]}})
```

```python
def pages(storage, collection, filter, size=100):
    skip = 0
    while True:
        rows = storage.run(action="find", collection=collection, filter=filter,
                           options={"limit": size, "skip": skip, "sort": {"_id": 1}})
        if not rows:
            return
        yield rows
        if len(rows) < size:
            return
        skip += size
```
