Filter operators for tables
Many Data API commands, like Find rows, accept a filter parameter to determine which rows to return or update. The filter parameter uses one or more filter operators.
You can use filter operators on all supported types except map
, list
, set
, and vector
.
Operators
You can use the following operators in a filter. All operators are case-sensitive.
Equal (default)
The $eq
operator matches rows where the specified column’s value is equal to the specified value.
This is the default when you do not specify an operator.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"number_of_pages": {"$eq": 300}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({ number_of_pages: { $eq: 300 } });
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.eq("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$eq": 300}}
}
}'
Not equal
The $ne
operator matches rows where the specified column’s value is not equal to the specified value.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"number_of_pages": {"$ne": 300}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({ number_of_pages: { $ne: 300 } });
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.ne("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$ne": 300}}
}
}'
Greater than
The $gt
operator matches rows where the specified column’s value is greater than the specified value.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"number_of_pages": {"$gt": 300}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({ number_of_pages: { $gt: 300 } });
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.gt("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$gt": 300}}
}
}'
Greater than or equal
The $gte
operator matches rows where the specified column’s value is greater than or equal to the specified value.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"number_of_pages": {"$gte": 300}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({ number_of_pages: { $gte: 300 } });
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.gte("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$gte": 300}}
}
}'
Less than
The $lt
operator matches rows where the specified column’s value is less than the specified value.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"number_of_pages": {"$lt": 300}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({ number_of_pages: { $lt: 300 } });
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.lt("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$lt": 300}}
}
}'
Less than or equal
The $lte
operator matches rows where the specified column’s value is less than or equal to the specified value.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"number_of_pages": {"$lte": 300}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({ number_of_pages: { $lte: 300 } });
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.lte("number_of_pages", 300);
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"number_of_pages": {"$lte": 300}}
}
}'
In
The $in
operator matches rows where the specified column’s value contains any of the specified values.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"author": {"$in": ["Sara Jones", "Jennifer Ray"]}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({
author: { $in: ["Sara Jones", "Jennifer Ray"] },
});
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.in("author", "Sara Jones", "Jennifer Ray");
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"author": {"$in": ["Sara Jones", "Jennifer Ray"]}}
}
}'
If you have only one value to match, you can use the single value instead of an array, like { "$in": "VALUE" }
.
Not in
The $nin
operator matches rows where the specified column’s value doesn’t contain any of the specified values.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one({"author": {"$nin": ["Sara Jones", "Jennifer Ray"]}})
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({
author: { $nin: ["Sara Jones", "Jennifer Ray"] },
});
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter = Filters.nin("author", "Sara Jones", "Jennifer Ray");
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"author": {"$nin": ["Sara Jones", "Jennifer Ray"]}}
}
}'
Combine operators (And, Or)
The $and
operator joins clauses with a logical AND
.
Only rows that match the conditions of all clauses are returned.
The $or
operator joins clauses with a logical OR
.
Rows that match the condition of any of the clauses are returned.
You can use $and
and $or
separately or together.
If you want to match multiple flexible conditions, use $and
and $or
together in the same filter:
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one(
{
"$and": [
{"$or": [{"is_checked_out": False}, {"number_of_pages": {"$lt": 300}}]},
{
"$or": [
{"author": {"$in": ["Sara Jones", "Jennifer Ray"]}},
{"publication_year": {"$gte": 2002}},
]
},
]
}
)
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({
$and: [
{
$or: [{ is_checked_out: false }, { number_of_pages: { $lt: 300 } }],
},
{
$or: [
{ genres: { $in: ["Sara Jones", "Jennifer Ray"] } },
{ publication_year: { $gte: 2002 } },
],
},
],
});
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
Filters.and(
Filters.or(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300)),
Filters.or(
Filters.in("author", "Sara Jones", "Jennifer Ray"),
Filters.gte("publication_year", 2002)));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {
"$and": [
{
"$or": [
{ "is_checked_out": false },
{"number_of_pages": {"$lt": 300}}
]
},
{
"$or": [
{"author": {"$in": ["Sara Jones", "Jennifer Ray"]}},
{"publication_year": {"$gte": 2002}}
]
}
]
}
}
}'
To filter on a range of values, use $and
with $lt
/$lte
and $gt
/$gte
:
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing table
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
table = database.get_table("TABLE_NAME")
# Find a row
result = table.find_one(
{
"$and": [
{"number_of_pages": {"$lt": 300}},
{"number_of_pages": {"$gte": 200}},
]
}
)
print(result)
import { DataAPIClient } from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const table = database.table("TABLE_NAME");
// Find a row
(async function () {
const result = await table.findOne({
$and: [
{ number_of_pages: { $lt: 300 } },
{ number_of_pages: { $gte: 200 } },
],
});
console.log(result);
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Optional;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Find a row
Filter filter =
Filters.and(Filters.lt("number_of_pages", 300), Filters.gte("number_of_pages", 200));
Optional<Row> result = table.findOne(filter);
System.out.println(result);
}
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"findOne": {
"filter": {"$and": [
{"number_of_pages": {"$lt": 300}},
{"number_of_pages": {"$gte": 200}}
]}
}
}'