# Collections

Collections are similar to what Excel users would called a Lookup, and you'll use them to extract variables from large datasets based on the inputs you’ve already been given.

For example, using a collection, you'd look down the first column of a dataset to find your key in a series of ABI codes, and you might then pick one particular rating from a larger table.

<figure><img src="https://1110149073-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fu89vYHjuY9Vj4vhZX5IZ%2Fuploads%2FnCdj9TuWY8sOaSZjrmnV%2FScreenshot%202024-01-26%20at%2009.49.09.png?alt=media&#x26;token=c2cf7a26-ebc7-4819-a1d7-d9d204c6b066" alt=""><figcaption><p>A completed Collection step</p></figcaption></figure>

## An Example Collection

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td></tr><tr><td>sue</td><td>24</td><td>false</td></tr><tr><td>jack</td><td>40</td><td>true</td></tr></tbody></table>

## Example formats

<pre class="language-javascript"><code class="lang-javascript"><strong>'collection.map(age)' // [30, 24, 40]
</strong>'collection.map(age).first()' // 30
'collection.map(age).last()' // 40
'collection.filter(age&#x3C;30).map(age).first()' // 24
'collection.filter(is_male=true).filter(age>30).map(name)first()' // jack
</code></pre>

The output must resolve to a value (e.g. number, string, boolean, date). Collection queries must start with the word ***'collection'.***

## Using inputs from previous steps

The input value is wrapped in `{{ }}`**.**&#x20;

For example, if the input is called `customer_age` and is equal to 30 in the quote, then:

<pre class="language-javascript"><code class="lang-javascript">'collection.filter(age&#x3C;{{customer_age}}).map(age).first()' // 24

// resolves to
<strong>
</strong><strong>'collection.filter(age&#x3C;30).map(age).first()' // 24
</strong>
</code></pre>

## Methods for use in Collections&#x20;

#### `count()`

Counts the number of items in an response. It can be used after a filter has been applied.

*Example:*

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td></tr><tr><td>sue</td><td>24</td><td>false</td></tr><tr><td>jack</td><td>40</td><td>true</td></tr></tbody></table>

```javascript
'collection.count()' // 3
'collection.filter(age<30).count()' // 1
'collection.filter(is_male=true).filter(age>25).count()' // 2
```

#### `min(), max(), mean(), range(), sum()`

Finding the min number, max number, mean number, sum number and range between the two.

*Example:*

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td></tr><tr><td>sue</td><td>24</td><td>false</td></tr><tr><td>jack</td><td>40</td><td>true</td></tr></tbody></table>

```javascript
'collection.map(age).min()' // 24
'collection.map(age).max()' // 40
'collection.map(age).range()' // 16 i.e. 40 - 24
'collection.map(age).mean()' // 31.3333333333
'round(collection.map(age).mean(), 2)' // 31.33
'collection.map(age).sum()' // 94

```

#### `map()`

Returns an array of the selected column within a collection of items. You can not return an array but you can have it as a chained step to do work on a collection.

*Example*:

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td></tr><tr><td>sue</td><td>24</td><td>false</td></tr><tr><td>jack</td><td>40</td><td>true</td></tr></tbody></table>

```javascript
'collection.map(age)' // [30, 24, 40]
'collection.map(name)' // ["bob", "sue", "jack"]

// You must resolve the array to make an output

'collection.map(name).first()' // "bob"
```

#### `filter()`

Returns a filtered collection based on a columns criteria.

Filters will work on columns with strings, booleans and numbers.  Filters can be chained.

You can also filter against value or empty/null data with the \~ flag&#x20;

*Example*:

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th><th>surname</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td><td>brown</td></tr><tr><td>sue</td><td>24</td><td>false</td><td></td></tr><tr><td>jack</td><td>40</td><td>true</td><td>brown</td></tr></tbody></table>

```javascript

'collection.filter(age>30).map(name)' // ["jack"]
'collection.filter(age<40).filter(is_male=true).map(name)' // ["bob"]
'collection.filter(age<40).filter(age>24).map(name)' // ["bob"]
'collection.filter(surname=brown~).count()' // 3 as the ~ returns empty cells too

// You must resolve the array to make an output
'collection.filter(age>30).map(name).first()' // "jack"

```

#### `unique`

Returns an array of the unique items from a column

*Example:*

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td></tr><tr><td>sue</td><td>24</td><td>false</td></tr><tr><td>jack</td><td>40</td><td>true</td></tr></tbody></table>

```javascript
'collection.unique(age).count()' // 3
'collection.unique(age).map(age).sum()' // 94
'collection.unique(is_male).count()' // 2
```

#### `first, last`

Returns an first or last element of an array as a value. This is a common way to resolve a result.

*Example:*

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td></tr><tr><td>sue</td><td>24</td><td>false</td></tr><tr><td>jack</td><td>40</td><td>true</td></tr></tbody></table>

```javascript
'collection.map(age).first()' // 30
'collection.map(age).last()' // 40
'collection.filter(age<30).map(age).first()' // 24
```

#### `exists`

Checks if property value exists i.e. not null, undefined, empty string or 0.

*Example:*

<table><thead><tr><th width="231">name</th><th>age</th><th>is_male</th></tr></thead><tbody><tr><td>bob</td><td>30</td><td>true</td></tr><tr><td>sue</td><td>24</td><td>false</td></tr><tr><td>jack</td><td>40</td><td>true</td></tr></tbody></table>

```javascript
'collection.filter(age>50).exists()' // "false"
'collection.filter(age<50).exists()' // "true"
```

#### `date, age`

If property is a date, then you can use date() to find a hour, date, month or year from the date string, or use age() to find the age since now in hours, days, months or years.

You can format a date from US or UK date formats.

*Example*:

<table><thead><tr><th width="231">name</th><th>dob</th><th>is_male</th><th>dob_format</th></tr></thead><tbody><tr><td>bob</td><td>1993-12-12</td><td>true</td><td>12/12/1993</td></tr><tr><td>sue</td><td>1999-01-01</td><td>false</td><td>01/01/1999</td></tr><tr><td>jack</td><td>1983-11-11</td><td>true</td><td>11/11/1983</td></tr></tbody></table>

```javascript
'collection.map(dob).first().date(YY)' // 1993
'collection.map(dob).first().date(MM)' // 12
'collection.map(dob).first().date(DD)' // 12
'collection.map(dob_format).first().date(UK)' // 1993-12-12
'collection.map(dob_format).first().date(US)' // 1993-12-12

'collection.map(dob).first().age(YY)' // 30
'collection.map(dob).first().age(MM)' // 360
'collection.map(dob).first().age(DD)' // 131800
```

A second argument can be provided to point at a date in the object instead of using now.&#x20;

```javascript
'collection.map(dob).first().age(YY, "2020-01-01")' // 27
```

Date and age work with arrays.

```javascript
'collection.map(dob).age(YY)' // [ 30 , 24, 40 ]
```

#### `postcode`

If property is a string postcode, then you can find the area and sector.

Example:

<table><thead><tr><th width="231">name</th><th>dob</th><th>postcode</th></tr></thead><tbody><tr><td>bob</td><td>1993-12-12</td><td>SW1A1AA</td></tr><tr><td>sue</td><td>1999-01-01</td><td>L201AR</td></tr><tr><td>jack</td><td>1983-11-11</td><td>M123RT</td></tr></tbody></table>

```javascript
'collection.map(postcode).first().postcode(area)'); // "SW"
'collection.map(postcode).first().postcode(sector)'); // "SW1A"
'collection.map(postcode).postcode(area)'); // ["SW", "L", "M"]
```

#### `regex`

If property is a string, then you can use regex() to parse a regular expression against the string and return the first match.

Example:

```javascript
'collection.map(postcode).first().regex(^[A-Za-z]{2}|^[A-Za-z]{1})'); // W
'collection.map(postcode).first().regex(^[A-Za-z]{2}|^[A-Za-z]{1})'); // PO
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://swallow-1.gitbook.io/swallow/your-first-model/essential-concepts/collections.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
