> For the complete documentation index, see [llms.txt](https://codemash.gitbook.io/codemash/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://codemash.gitbook.io/codemash/cloud/database/aggregates/edit.md).

# Edit an aggregate

Change a saved aggregate and test it again — apps that run it get the new answer at once.

**Where:** Database → Aggregates → click the **ID** · `/projects/<project>/db/aggregates/<aggregateId>/edit`

## Before you start

* The aggregate exists — see [Create an aggregate](/codemash/cloud/database/aggregates/new.md).
* You need `database:update` on the aggregates of its schema; **TEST** also needs read on every joined collection.

## Steps

1. Open **Database → Aggregates** and click the aggregate's id. The editor opens as **Editing aggregate**, with **Delete** at the top right. The **Schema** is shown as text: it cannot be changed. When the pipeline joins other collections, **Joined collections** lists them.

   <figure><img src="https://712692422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHy372bqsaex2cqS3ikhL%2Fuploads%2Fgit-blob-d1ecbb47c73b66b6504b11775329589b5a61d36a%2Faggregates-edit-1.png?alt=media" alt="Editing aggregate: Schema books (fixed), Name Books per genre, Description, the Sample record panel; a Delete button at the top right"><figcaption><p>A saved aggregate in the editor.</p></figcaption></figure>
2. Change **Name**, **Description** or the pipeline — for example add `{ "$limit": 2 }` as the last stage.
3. Press **TEST** to run the changed pipeline on live data.
4. Press **Save**.

## Result

Cloud goes back to the list. The aggregate keeps its id, so the next `executeAggregate` call with `maggr_5W9f2eefrp0I3UwXVxDP2o` returns the new answer — no app release needed. Saving again also refreshes the stored list of joined collections.

When the project has an AI assistant set up, **Edit using AI Chat** lets it rewrite the open pipeline; you review it and press **Save**.

## Change or remove it later

* **Delete** — see [Delete an aggregate](/codemash/cloud/database/aggregates/delete.md).
* To run the same pipeline on another collection, [create a new aggregate](/codemash/cloud/database/aggregates/new.md) for it.

## Do it from code

```ts
await norbix.hub.database.saveDatabaseAggregate({
  viewId: id, // maggr_… — without it a new aggregate is created
  schemaId: 'sch_7lPCzWonEvTQsheGDCaKq',
  displayName: 'Top 2 genres',
  pipeline: JSON.stringify([{ $group: { _id: '$genre', books: { $sum: 1 } } }, { $sort: { books: -1 } }, { $limit: 2 }]),
});
```

```sh
norbix api database aggregate execute --collectionName books --aggregateId maggr_5W9f2eefrp0I3UwXVxDP2o
```

The client setup is on the [Database page](/codemash/cloud/database.md#use-it-from-code). API: [Save Database Aggregate](/codemash/api-reference/database/aggregates/save-database-aggregate.md) · [Get Database Aggregate](/codemash/api-reference/database/aggregates/get-database-aggregate.md) · [Execute Aggregate](/codemash/api-reference/database/collections/execute-aggregate.md)
