> ## Documentation Index
> Fetch the complete documentation index at: https://developers.fibery.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Collections

> Learn how to manage collection, single-select, and multi-select Fields using the Fibery API.

<Note>
  The API uses `type` for Database and `app` for Space. See [Terminology](/guides/general/terminology#api-naming).
</Note>

## Update entity collection Field

Add already existing Entities to an entity collection Field by providing their `fibery/id`. Remove Entities from the collection in a similar way.

Get `fibery/id` either via API or by opening the relevant Entity on UI and exploring the command response in browser's Network tab.

`Cricket/Player` Database used as an example

| Field name             | Field type              |
| ---------------------- | ----------------------- |
| `fibery/id`            | `fibery/uuid`           |
| `Cricket/Former Teams` | entity collection Field |

### Add

Add two existing Teams to Player's "Former Teams" entity collection Field (if team already exists in the collection then team will be ignored during addition):

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch('https://YOUR_ACCOUNT.fibery.io/api/commands', {
    method: 'POST',
    headers: {
      'Authorization': 'Token YOUR_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      command: 'fibery.entity/add-collection-items',
      args: {
        type: 'Cricket/Player',
        field: 'Cricket/Former Teams',
        entity: {
          '216c2a00-9752-11e9-81b9-4363f716f666': [
            '0a3ae1c0-97fa-11e9-81b9-4363f716f666',
            '17af8db0-97fa-11e9-81b9-4363f716f666'
          ]
        }
      }
    })
  });
  const data = await response.json();
  ```

  ```bash cURL theme={null}
  curl -X POST https://YOUR_ACCOUNT.fibery.io/api/commands \
    -H 'Authorization: Token YOUR_TOKEN' \
    -H 'Content-Type: application/json' \
    -d '
     {
       "command": "fibery.entity/add-collection-items",
       "args": {
         "type": "Cricket/Player",
         "field": "Cricket/Former Teams",
         "entity": {
           "216c2a00-9752-11e9-81b9-4363f716f666": [
             "0a3ae1c0-97fa-11e9-81b9-4363f716f666",
             "17af8db0-97fa-11e9-81b9-4363f716f666"
           ]
         }
       }
     }
    '
  ```
</CodeGroup>

Response:

```json theme={null}
{
  "success": true,
  "result": "ok"
}
```

### Remove

Remove two Teams from Player's "Former Teams" entity collection Field (if team doesn't exist in the collection then team will be ignored during removing):

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch('https://YOUR_ACCOUNT.fibery.io/api/commands', {
    method: 'POST',
    headers: {
      'Authorization': 'Token YOUR_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      command: 'fibery.entity/remove-collection-items',
      args: {
        type: 'Cricket/Player',
        field: 'Cricket/Former Teams',
        entity: {
          '216c2a00-9752-11e9-81b9-4363f716f666': [
            '0a3ae1c0-97fa-11e9-81b9-4363f716f666',
            '17af8db0-97fa-11e9-81b9-4363f716f666'
          ]
        }
      }
    })
  });
  const data = await response.json();
  ```

  ```bash cURL theme={null}
  curl -X POST https://YOUR_ACCOUNT.fibery.io/api/commands \
    -H 'Authorization: Token YOUR_TOKEN' \
    -H 'Content-Type: application/json' \
    -d '
     {
       "command": "fibery.entity/remove-collection-items",
       "args": {
         "type": "Cricket/Player",
         "field": "Cricket/Former Teams",
         "entity": {
           "216c2a00-9752-11e9-81b9-4363f716f666": [
             "0a3ae1c0-97fa-11e9-81b9-4363f716f666",
             "17af8db0-97fa-11e9-81b9-4363f716f666"
           ]
         }
       }
     }
    '
  ```
</CodeGroup>

Response:

```json theme={null}
{
  "success": true,
  "result": "ok"
}
```

### Set

Replace two Teams from Player's "Former Teams" entity collection Field with new team.

<Callout icon="circle-exclamation" color="#199EE3">
  Replace means deletion of any existing collection items and adding new items.
</Callout>

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch('https://YOUR_ACCOUNT.fibery.io/api/commands', {
    method: 'POST',
    headers: {
      'Authorization': 'Token YOUR_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      command: 'fibery.entity/set-collection-items',
      args: {
        type: 'Cricket/Player',
        field: 'Cricket/Former Teams',
        entity: {
          '216c2a00-9752-11e9-81b9-4363f716f666': [
            '02007d5e-b3d9-44c3-83de-6b562870f120'
          ]
        }
      }
    })
  });
  const data = await response.json();
  ```

  ```bash cURL theme={null}
  curl -X POST https://YOUR_ACCOUNT.fibery.io/api/commands \
    -H 'Authorization: Token YOUR_TOKEN' \
    -H 'Content-Type: application/json' \
    -d '
     {
       "command": "fibery.entity/set-collection-items",
       "args": {
         "type": "Cricket/Player",
         "field": "Cricket/Former Teams",
         "entity": {
           "216c2a00-9752-11e9-81b9-4363f716f666": [
             "02007d5e-b3d9-44c3-83de-6b562870f120"
           ]
         }
       }
     }
    '
  ```
</CodeGroup>

Response:

```json theme={null}
{
  "success": true,
  "result": "ok"
}
```

### Reset

Resets "Former Teams" entity collection Field with new team.

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch('https://YOUR_ACCOUNT.fibery.io/api/commands', {
    method: 'POST',
    headers: {
      'Authorization': 'Token YOUR_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      command: 'fibery.entity/reset-collection-items',
      args: {
        type: 'Cricket/Player',
        field: 'Cricket/Former Teams',
        entity: '216c2a00-9752-11e9-81b9-4363f716f666'
      }
    })
  });
  const data = await response.json();
  ```

  ```bash cURL theme={null}
  curl -X POST https://YOUR_ACCOUNT.fibery.io/api/commands \
    -H 'Authorization: Token YOUR_TOKEN' \
    -H 'Content-Type: application/json' \
    -d '
     {
       "command": "fibery.entity/reset-collection-items",
       "args": {
         "type": "Cricket/Player",
         "field": "Cricket/Former Teams",
         "entity": "216c2a00-9752-11e9-81b9-4363f716f666"
       }
     }
    '
  ```
</CodeGroup>

Response:

```json theme={null}
{
  "success": true,
  "result": "ok"
}
```

## Update single-select and multi-select Fields

A single-select Field is an entity Field — each option is an Entity with its own `fibery/id`. Update it via `fibery.entity/update`, the same way as any entity Field.

A multi-select Field is an entity collection Field — each option is an Entity with its own `fibery/id`. Update it via collection commands (`add-collection-items`, `remove-collection-items`, `set-collection-items`, `reset-collection-items`), the same way as any entity collection Field.

Get an option's `fibery/id` either via API or by opening the Entity on UI and exploring the command response in browser's Network tab.

`Cricket/Player` Database used as an example

| Field name              | Field type    |
| ----------------------- | ------------- |
| `Cricket/Batting Hand`  | single-select |
| `Cricket/Playing Roles` | multi-select  |

### Set single-select Field

Set `Cricket/Batting Hand` on a Player:

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch('https://YOUR_ACCOUNT.fibery.io/api/commands', {
    method: 'POST',
    headers: {
      'Authorization': 'Token YOUR_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      command: 'fibery.entity/update',
      args: {
        type: 'Cricket/Player',
        entity: {
          'fibery/id': '20f9b920-9752-11e9-81b9-4363f716f666',
          'Cricket/Batting Hand': { 'fibery/id': 'b0ed1370-9747-11e9-9f03-fd937c4ecf3b' }
        }
      }
    })
  });
  const data = await response.json();
  ```

  ```bash cURL theme={null}
  curl -X POST https://YOUR_ACCOUNT.fibery.io/api/commands \
    -H 'Authorization: Token YOUR_TOKEN' \
    -H 'Content-Type: application/json' \
    -d '
     {
       "command": "fibery.entity/update",
       "args": {
         "type": "Cricket/Player",
         "entity": {
           "fibery/id": "20f9b920-9752-11e9-81b9-4363f716f666",
           "Cricket/Batting Hand": {"fibery/id": "b0ed1370-9747-11e9-9f03-fd937c4ecf3b"}
         }
       }
     }
    '
  ```
</CodeGroup>

Response:

```json theme={null}
{
  "success": true,
  "result": {
    "fibery/id": "20f9b920-9752-11e9-81b9-4363f716f666"
  }
}
```

### Add options to multi-select Field

Add two options to `Cricket/Playing Roles` on a Player:

<CodeGroup>
  ```javascript JavaScript theme={null}
  const response = await fetch('https://YOUR_ACCOUNT.fibery.io/api/commands', {
    method: 'POST',
    headers: {
      'Authorization': 'Token YOUR_TOKEN',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      command: 'fibery.entity/add-collection-items',
      args: {
        type: 'Cricket/Player',
        field: 'Cricket/Playing Roles',
        entity: {
          '20f9b920-9752-11e9-81b9-4363f716f666': [
            'c1d8e4b0-9747-11e9-9f03-fd937c4ecf3b',
            'c1d9f5c0-9747-11e9-9f03-fd937c4ecf3b'
          ]
        }
      }
    })
  });
  const data = await response.json();
  ```

  ```bash cURL theme={null}
  curl -X POST https://YOUR_ACCOUNT.fibery.io/api/commands \
    -H 'Authorization: Token YOUR_TOKEN' \
    -H 'Content-Type: application/json' \
    -d '
     {
       "command": "fibery.entity/add-collection-items",
       "args": {
         "type": "Cricket/Player",
         "field": "Cricket/Playing Roles",
         "entity": {
           "20f9b920-9752-11e9-81b9-4363f716f666": [
             "c1d8e4b0-9747-11e9-9f03-fd937c4ecf3b",
             "c1d9f5c0-9747-11e9-9f03-fd937c4ecf3b"
           ]
         }
       }
     }
    '
  ```
</CodeGroup>

Response:

```json theme={null}
{
  "success": true,
  "result": "ok"
}
```

Remove, replace, or reset multi-select options the same way — see [Update entity collection Field](#update-entity-collection-field) for `fibery.entity/remove-collection-items`, `fibery.entity/set-collection-items`, and `fibery.entity/reset-collection-items`.
