> ## Documentation Index
> Fetch the complete documentation index at: https://upstash-dx-3081-agentkit-tanstack-ai-backends.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# $cardinality

`$cardinality` returns the number of distinct values in a field.

All matching field values are deduplicated, then counted.

If `missing` is provided, documents without the field are treated as that fallback key
(which can increase the distinct count by at most one extra value).

### Compatibility

| Field Type | Supported |
| - | - |
| TEXT | No |
| U64/I64/F64 | Yes |
| DATE | Yes |
| BOOL | Yes |
| KEYWORD | Yes |
| FACET | No |

Field must be `FAST`.

### Arguments

| Argument | Type | Required | Description |
| - | - | - | - |
| `field` | `string` | Yes | Field to aggregate. |
| `missing` | `string \| number` | No | Fallback key for missing fields. |

<Tabs>
  <Tab title="TypeScript">
    ```ts theme={null}
    await index.aggregate({
      aggregations: {
        unique_users: { $cardinality: { field: "userId" } },
      },
    });
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    index.aggregate(
        aggregations={"unique_users": {"$cardinality": {"field": "userId"}}}
    )
    ```
  </Tab>

  <Tab title="Redis CLI">
    ```bash theme={null}
    SEARCH.AGGREGATE events '{}' '{"unique_users": {"$cardinality": {"field": "userId"}}}'
    ```
  </Tab>
</Tabs>

### Output

```json theme={null}
{ "unique_categories": { "value": 3 } }
```


## Related topics

- [SCARD](/redis/commands/set/scard.md)
- [Set commands](/redis/commands/set/overview.md)
- [SINTERCARD](/redis/commands/set/sintercard.md)
- [ZINTERCARD](/redis/commands/sorted-set/zintercard.md)
- [PFCOUNT](/redis/commands/hyperloglog/pfcount.md)
