---
title: "Logic"
description: "Master Aerospike logic expressions including AND, OR, NOT, and EXCLUSIVE for advanced record filtering and path queries."
---

# Logic

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

Logic expressions combine Boolean subexpressions with `and`, `or`, `not`, and `exclusive` (needs server 5.6.0+). The result is Boolean and is used in the same places as [comparison](https://aerospike.com/docs/develop/expressions/comparison) expressions: record filters, query and batch `FilterExpression`, [XDR filters](https://aerospike.com/docs/database/manage/xdr/filters/), and other APIs that accept filter expressions.

`and` / `or` / `not` follow trilean rules when inputs can be `unknown` (for example during the metadata-only phase of evaluation). `exclusive` tests whether exactly one of its Boolean arguments is `true`. For `or`, if at least one child is `true`, the result is `true` even when other children are `unknown`. For how `true`, `false`, and `unknown` interact otherwise, see the [execution model](https://aerospike.com/docs/develop/expressions/#execution-model) on the expressions overview and [`unknown()`](https://aerospike.com/docs/develop/expressions/declare#unknown).

### Path expressions

`and` and `or` appear frequently inside [path expression](https://aerospike.com/docs/develop/expressions/path/) filter contexts such as `allChildrenWithFilter` and `andFilter`. See [combining multiple filters](https://aerospike.com/docs/develop/expressions/path/advanced#combine-multiple-filters) in the path advanced guide.

The [bookstore example](https://aerospike.com/docs/develop/expressions/path#bookstore-example) on the path overview shows `Exp.and` with nested `eq` and `lt` on loop variables—typical of real document-style filters.

## Ops

#### `and`

```python
and(arg0, arg1, ...)
```

Description: Returns `false` if any ‘boolean\_expr’ is `false`; otherwise, it returns `true`.

Arguments: | Name | Type |
| --- | --- |
| `arg0` | `boolean_expr` |
| `arg1` | `boolean_expr` |
| `...` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.2.0.4

Example: Find records that have a stored user key **and** that key is an integer less than 1000 (metadata + key comparison). For the same combinator on nested bins inside path filters, see the [bookstore example](https://aerospike.com/develop/expressions/path#bookstore-example).

-   [Java](#tab-panel-1249)
-   [Python](#tab-panel-1250)
-   [C](#tab-panel-1251)
-   [Go](#tab-panel-1252)
-   [C#](#tab-panel-1253)
-   [Node.js](#tab-panel-1254)

```java
Expression exp = Exp.build(

  Exp.and(

    Exp.keyExists(),

    Exp.lt(Exp.key(Exp.Type.INT), Exp.val(1000))));
```

```python
from aerospike_helpers.expressions import And, KeyExists, KeyInt, LT

exp = And(KeyExists(), LT(KeyInt(), 1000)).compile()
```

```c
as_exp_build(predexp,

  as_exp_and(as_exp_key_exist(),

    as_exp_cmp_lt(as_exp_key_int(), as_exp_int(1000))));
```

```go
// Import path matches the Go client examples elsewhere on this doc site (/v6).

// If you use the latest client line, switch to .../aerospike-client-go/v8 (API names unchanged).

// Requires: import as "github.com/aerospike/aerospike-client-go/v6"

exp := as.ExpAnd(

  as.ExpKeyExists(),

  as.ExpLess(as.ExpKey(as.ExpTypeINT), as.ExpIntVal(1000)))
```

```csharp
Expression exp = Exp.Build(

  Exp.And(

    Exp.KeyExists(),

    Exp.LT(Exp.Key(Exp.Type.INT), Exp.Val(1000))));
```

```javascript
const exp = Aerospike.exp

const filterExp = exp.and(

  exp.keyExist(),

  exp.lt(exp.keyInt(), exp.int(1000)))
```

---

#### `exclusive`

```python
exclusive(arg0, arg1, ...)
```

Description: Returns `true` if exactly one ‘boolean\_expr’ is `true`; otherwise, it returns `false`. This expression is helpful for testing whether multiple expressions are mutually exclusive.

Arguments: | Name | Type |
| --- | --- |
| `arg0` | `boolean_expr` |
| `arg1` | `boolean_expr` |
| `...` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.6.0

Example: Find records where **exactly one** of these is true: string bin `hand` is `hook`, `leg` is `peg`, or `pet` is `parrot`. (Illustrates mutual-exclusion checks on independent bins—not nested path logic.)

-   [Java](#tab-panel-1255)
-   [Python](#tab-panel-1256)
-   [C](#tab-panel-1257)
-   [Go](#tab-panel-1258)
-   [C#](#tab-panel-1259)
-   [Node.js](#tab-panel-1260)

```java
Expression exp = Exp.build(

  Exp.exclusive(

    Exp.eq(Exp.stringBin("hand"), Exp.val("hook")),

    Exp.eq(Exp.stringBin("leg"), Exp.val("peg")),

    Exp.eq(Exp.stringBin("pet"), Exp.val("parrot"))));
```

```python
from aerospike_helpers.expressions import Eq, Exclusive, StrBin

exp = Exclusive(

    Eq(StrBin("hand"), "hook"),

    Eq(StrBin("leg"), "peg"),

    Eq(StrBin("pet"), "parrot"),

).compile()
```

```c
as_exp_build(predexp,

  as_exp_exclusive(

    as_exp_cmp_eq(as_exp_bin_str("hand"), as_exp_str("hook")),

    as_exp_cmp_eq(as_exp_bin_str("leg"), as_exp_str("peg")),

    as_exp_cmp_eq(

      as_exp_bin_str("pet"), as_exp_str("parrot"))));
```

```go
// Import path matches the Go client examples elsewhere on this doc site (/v6).

// If you use the latest client line, switch to .../aerospike-client-go/v8 (API names unchanged).

// Requires: import as "github.com/aerospike/aerospike-client-go/v6"

exp := as.ExpExclusive(

  as.ExpEq(as.ExpStringBin("hand"), as.ExpStringVal("hook")),

  as.ExpEq(as.ExpStringBin("leg"), as.ExpStringVal("peg")),

  as.ExpEq(as.ExpStringBin("pet"), as.ExpStringVal("parrot")))
```

```csharp
Expression exp = Exp.Build(

  Exp.Exclusive(

    Exp.EQ(Exp.StringBin("hand"), Exp.Val("hook")),

    Exp.EQ(Exp.StringBin("leg"), Exp.Val("peg")),

    Exp.EQ(Exp.StringBin("pet"), Exp.Val("parrot"))));
```

```javascript
const exp = Aerospike.exp

const filterExp = exp.exclusive(

  exp.eq(exp.binStr('hand'), exp.str('hook')),

  exp.eq(exp.binStr('leg'), exp.str('peg')),

  exp.eq(exp.binStr('pet'), exp.str('parrot')))
```

---

#### `not`

```python
not(arg)
```

Description: Returns `true` if the ‘boolean\_expr’ is `false`; otherwise, it returns `false`.

Arguments: | Name | Type |
| --- | --- |
| `arg` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.2.0.4

Example: Find records that do not have the primary key stored in record metadata (`keyExists` is false).

-   [Java](#tab-panel-1261)
-   [Python](#tab-panel-1262)
-   [C](#tab-panel-1263)
-   [Go](#tab-panel-1264)
-   [C#](#tab-panel-1265)
-   [Node.js](#tab-panel-1266)

```java
Expression exp = Exp.build(Exp.not(Exp.keyExists()));
```

```python
from aerospike_helpers.expressions import KeyExists, Not

exp = Not(KeyExists()).compile()
```

```c
as_exp_build(predexp, as_exp_not(as_exp_key_exist()));
```

```go
// Import path matches the Go client examples elsewhere on this doc site (/v6).

// If you use the latest client line, switch to .../aerospike-client-go/v8 (API names unchanged).

// Requires: import as "github.com/aerospike/aerospike-client-go/v6"

exp := as.ExpNot(as.ExpKeyExists())
```

```csharp
Expression exp = Exp.Build(Exp.Not(Exp.KeyExists()));
```

```javascript
const exp = Aerospike.exp

const filterExp = exp.not(exp.keyExist())
```

---

#### `or`

```python
or(arg0, arg1, ...)
```

Description: Returns `true` if any ‘boolean\_expr’ is `true`; otherwise, it returns `false`.

Arguments: | Name | Type |
| --- | --- |
| `arg0` | `boolean_expr` |
| `arg1` | `boolean_expr` |
| `...` | `boolean_expr` |

Returns: `boolean_value`

Introduced: 5.2.0.4

Example: Find records where string bin `country` is either `US` or `CA` (same “one of several string values” idea as the [expression index tutorial](https://aerospike.com/database/learn/tutorials/quick-start-expression-index/), which uses `Or` / `Eq` for target countries in Python).

-   [Java](#tab-panel-1267)
-   [Python](#tab-panel-1268)
-   [C](#tab-panel-1269)
-   [Go](#tab-panel-1270)
-   [C#](#tab-panel-1271)
-   [Node.js](#tab-panel-1272)

```java
Expression exp = Exp.build(

  Exp.or(

    Exp.eq(Exp.stringBin("country"), Exp.val("US")),

    Exp.eq(Exp.stringBin("country"), Exp.val("CA"))));
```

```python
from aerospike_helpers.expressions import Eq, Or, StrBin

exp = Or(

    Eq(StrBin("country"), "US"),

    Eq(StrBin("country"), "CA"),

).compile()
```

```c
as_exp_build(predexp,

  as_exp_or(

    as_exp_cmp_eq(as_exp_bin_str("country"), as_exp_str("US")),

    as_exp_cmp_eq(

      as_exp_bin_str("country"), as_exp_str("CA"))));
```

```go
// Import path matches the Go client examples elsewhere on this doc site (/v6).

// If you use the latest client line, switch to .../aerospike-client-go/v8 (API names unchanged).

// Requires: import as "github.com/aerospike/aerospike-client-go/v6"

exp := as.ExpOr(

  as.ExpEq(as.ExpStringBin("country"), as.ExpStringVal("US")),

  as.ExpEq(as.ExpStringBin("country"), as.ExpStringVal("CA")))
```

```csharp
Expression exp = Exp.Build(

  Exp.Or(

    Exp.EQ(Exp.StringBin("country"), Exp.Val("US")),

    Exp.EQ(Exp.StringBin("country"), Exp.Val("CA"))));
```

```javascript
const exp = Aerospike.exp

const filterExp = exp.or(

  exp.eq(exp.binStr('country'), exp.str('US')),

  exp.eq(exp.binStr('country'), exp.str('CA')))
```

---