# select Parameter

> Choose the fields of an answer with the select parameter of API Maker - a list of columns to keep, or a list to leave out with a minus sign, on reads and on the answers of writes, with $slice and $elemMatch for the arrays of MongoDB.

Source: https://docs.apimaker.dev/v4/docs/apis-all/query-params/select.html

`select` keeps only the fields you name, or, with `-`, everything but the fields you name. Less data on the wire, and nothing a screen does not need.

**Only these fields**

```text
GET /api/schema/admin/mysql8/inventory/customers?select=first_name,last_name
```

**Answer : the primary key always comes along**

```json
{
    "success": true,
    "statusCode": 200,
    "data": [
        { "customer_id": 1, "first_name": "Bob", "last_name": "Lin" },
        { "customer_id": 2, "first_name": "Alice", "last_name": "Page" }
    ]
}
```

**Everything but these fields**

```text
GET /api/schema/admin/mysql8/inventory/customers?select=-last_update,-isActive
```

**Body of the query APIs : a string or an object**

```json
{ "find": {}, "select": "first_name,last_name" }
{ "find": {}, "select": { "first_name": 1, "last_name": 1 } }
```

- On MongoDB a nested field selects too : `select=address.city`.
- Do not mix included and excluded fields in one `select`.
- The write APIs (save, master save, update by id, replace by id, remove by id) accept `select` to shape the row they answer with.
- Inside a `deep` item, `select` picks the fields of the related rows : `deep=[{s_key:'customer_id',t_col:'orders',t_key:'customer_id',select:'order_no,total'}]`.
- A field the group of the caller can not read never comes back, selected or not : that is the field permission of [API group permission](https://docs.apimaker.dev/v4/docs/apis-security/api-group-permission.html).

## Arrays on MongoDB : $slice and $elemMatch

A field of a MongoDB document can hold an array. `select` takes the two operators MongoDB has to shape one, on the generated and on the schema APIs :

| Operator | Example | The array comes back with |
|---|---|---|
| `$slice` | `{ "tags": { "$slice": 2 } }` | its first 2 items, its last 2 with `-2` |
| `$slice` | `{ "orders": { "$slice": [ 10, 5 ] } }` | 5 items after the first 10 : `[ skip, limit ]` |
| `$elemMatch` | `{ "orders": { "$elemMatch": { "status": "PAID" } } }` | its first item which meets the conditions |

**The last 3 orders of each customer**

```json
{ "find": {}, "select": { "first_name": 1, "orders": { "$slice": -3 } } }
```

**As a query param (JSON5)**

```text
GET /api/schema/admin/mongodb/inventory/customers?select={first_name:1,orders:{$slice:-3}}
```

- Next to fields with `1`, those fields and the array come back. Alone, `$slice` leaves every other field in, and `$elemMatch` returns the array with the id only, as MongoDB does.
- The conditions of `$elemMatch` are the ones of [find](https://docs.apimaker.dev/v4/docs/apis-all/query-params/find.html). The schema APIs convert their values to the types of the schema : `{ "order_id": "5" }` matches the number 5, an id string an ObjectId.
- `$elemMatch` is for an array at the top of the document. `$slice` cuts a nested one too : `{ "orders.items": { "$slice": 1 } }`.
- On the schema APIs `$slice` takes numbers only, and any other object in `select` selects the whole field : nothing is computed from other fields.
- `select` of a `deep` item and of the write APIs takes them too.
- A table of a SQL instance holds no arrays : there the field is selected as it is.

- [deep →](https://docs.apimaker.dev/v4/docs/apis-all/query-params/deep.html)
- [sort](https://docs.apimaker.dev/v4/docs/apis-all/query-params/query-params.html#sort)
