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

# Filtering And Sorting

Some of our endpoints support specifying filter or sorting criteria within the request. These options can reduce the result set size or return data in a more suitable order.

## Filtering

Zixflow supports two distinct formats for filtering: **shorthand** and **verbose**.

* **Shorthand Format**: Ideal for quick equality checks.
* **Verbose Format**: Allows for more complex conditions and logical operators.

***

## Shorthand Filters

Shorthand filters are straightforward and concise. Here's an example:

##### Example: People named "John Doe" with the email "[john@doe.com](mailto:john@doe.com)"

```http theme={null}
POST /api/v1/collection-records/{collectionId}/query
Content-Type: application/json

{
  "filter": {
    "name": "John Doe",
    "emails": "john@doe.com"
  }
}
```

## Verbose Filters

Verbose filters enable more complex queries, such as combining multiple conditions or querying specific properties of an attribute. All shorthand filters can also be expressed in verbose syntax.

##### Example: Using verbose syntax for the same filter

```http theme={null}
POST /api/v1/collection-records/{collectionId}/query
Content-Type: application/json

{
  "filter": {
    "$and": [
      {
        "name": {
            "$eq": "John Doe"
        }
      },
      {
        "emails": {
            "$eq": "john@doe.com"
        }
      }
    ]
  }
}
```

Here:

* A logical operator (`$and`) is used to combine conditions.
* Specific properties on the attributes (`name` and `emails`) are queried.

> Each attribute type has specific properties available, so the set of possible filters varies by attribute type.

***

## Comparison Operators

There are **fourteen comparison operators** in total. Below are some examples:

### `$eq` - Equality

Checks for equality and is supported by every attribute type. If using shorthand syntax, `$eq` is usually implied but can also be specified explicitly.

> Deals with an exact name

```json theme={null}
{
  "name": {
    "$eq": "John Doe"
  }
}
```

### `$not_empty`

Filters results based on whether there is any value defined.

> Companies which have at least one domain

```json theme={null}
{
  "name": {
    "$not_empty": true
  }
}
```

### `$in`

Checks if the record or entry has a value that is part of a set.

> Records where `record_id` is one of many values

```json theme={null}
{
  "filter": {
    "name": {
      "$in": ["John Doe", "John Doe 2"]
    }
  }
}
```

***

## String Comparisons

For string-like properties or attributes, the following operators are available:

### `$contains`

Matches parts of a string, case-sensitively.

> Name contains John

```json theme={null}
{
  "name": {
    "$contains": "John"
  }
}
```

### `$not_contains`

Do not matches parts of a string, case-sensitively.

> Name does not contains John

```json theme={null}
{
  "name": {
    "$not_contains": "John"
  }
}
```

### `$starts_with` & `$ends_with`

Matches the beginning or the end of a string, respectively.

> People with phone numbers starting `+1`

```json theme={null}
{
  "phoneNumbers": {
    "$starts_with": "+1"
  }
}
```

> Phone number starts with `+1` and ends with `0909`

```json theme={null}
{
  "phoneNumbers": {
    "$starts_with": "+1",
    "$ends_with": "0909"
  }
}
```

***

## Numeric or Date Comparisons

For sortable properties like numbers or dates, use the following operators:

* **`$lt`**: Less than
* **`$lte`**: Less than or equal
* **`$gte`**: Greater than or equal
* **`$gt`**: Greater than

> People with 4 or more rating

```json theme={null}
{
  "rating": {
    "$gte": 4
  }
}
```

> people created in 2024

```json theme={null}
{
  "createdAt": {
    "$gte": "2024-01-01",
    "$lte": "2024-12-31"
  }
}
```

***

## Logical Operators

Combine multiple conditions using `$and`, `$or`, and `$not`.

### `$and` Operator

Specifies that all conditions must match. If using shorthand syntax with multiple attributes, `$and` is implied.

> People with name John Doe and email is [john@doe.com](mailto:john@doe.com)

```json theme={null}
{
  "$and": [{ "name": "John Doe" }, { "emails": "john@doe.com" }]
}
```

### `$or` Operator

Specifies that at least one condition must match.

> List all peoples with name John Doe or Alice Doe

```json theme={null}
{
  "$or": [{ "name": "John Doe" }, { "name": "Alice Doe" }]
}
```

### `$not` Operator

Matches all documents that **do not** meet the condition.

> People not with name John

```json theme={null}
{
  "name": { "$not": "John" }
}
```

***

## Sorting

Sorting allows us to retrieve results in a specific order based on attribute values. Each sort operation must define a direction.

Sorting can be performed by an attribute apiKeyName. For instance, we can sort People first by their name and then by their email address:

```json theme={null}
{
  "sort": [
    { "direction": "asc", "attribute": "name" },
    { "direction": "desc", "attribute": "emails" }
  ]
}
```
