# total

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

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

Grup, uni, calcule — nella base di dati, e trae la risposta in lugar de filas.

Status: document database · free · 500 results · 30 s

Esta è l'azione che sostituisce un loop. Recolhere dez mil ordini per sommar i loro imports è dez mil documenti a travers del filo; la stessa domanda rispondu aquí è una fila.

## Parâmetros {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `collection` | string | obrigatório | De qua colezione il oleoducto parte. |
| `pipeline` | JSON text | obrigatório | Una lista non vuota di stadi. Vazio o ausente è negat. |

```javascript
await storage.run({
  action: "aggregate",
  collection: "orders",
  pipeline: JSON.stringify([
    { $match: { status: "paid", createdAt: { $gte: "2026-01-01" } } },
    { $group: { _id: "$country", total: { $sum: "$amount" }, orders: { $sum: 1 } } },
    { $sort: { total: -1 } },
    { $limit: 10 },
  ]),
});
// [{ _id: "FR", total: 184200, orders: 912 }, …]
```

> **Il canal è JSON text, e questo è l'onde più importa**
>
> Cada nome d'etape inizia con $. Passato come un objeto anidat attraverso un chillante che tira le chiavi, il pipeline arriva vazio de i propri stadi e l'appella non fa nulla per explicarlo.

## Due famiglie di stadio sono rifiutate {#refused}

| Refusi | Por quê |
| --- | --- |
| $where · $funcion · $acumulador | E executano codice dentro la base di dati. |
| - Usí · - Usí | Ecriven il risultato in una collezion — che non è un lect. |

Ambas sono catturate prima che il oleoducto è enviado, quindi un rejet non costa nulla e non atinge nulla. Scriver un risultato, legîlo e [inserire](/pt/sdk/ref/connected-storage/run-write/insert) it — che è un scrit, richiesto come un.

## I limites {#bounds}

| Limite | Valor |
| --- | --- |
| Results | 500 — mettere $limite nel pipeline per che scelse 500. |
| Tempo | 30 secondi. |

Un oleoducto che arrivi al tecto temporale solitamente vuole un `$match` prima: filtra, poi grup. Agrupare tutto e filtrare poi fa la parte costosa in tot.

- [trov](/pt/sdk/ref/connected-storage/run/find): Quand ce sont les documents qu'on veut.
- [run()](/pt/sdk/ref/connected-storage/run): Tutes les actions, les garde-fous et ce que chaque coute.

```python
import json

storage.run(
    action="aggregate",
    collection="orders",
    pipeline=json.dumps([
        {"$match": {"status": "paid", "createdAt": {"$gte": "2026-01-01"}}},
        {"$group": {"_id": "$country", "total": {"$sum": "$amount"}, "orders": {"$sum": 1}}},
        {"$sort": {"total": -1}},
        {"$limit": 10},
    ]),
)
# [{"_id": "FR", "total": 184200, "orders": 912}, …]
```
