Filter operators for collections (Python)

Many Data API commands, like Find documents (Python), accept a filter parameter to determine which documents to return or update. The filter parameter uses one or more filter operators.

You must use & to escape any . or & in field names in a filter. Dot notation, which is used to reference nested fields, should not be escaped. For more information, see Work with . and & in field names (Python).

Operators

You can use the following operators in a filter. All operators are case-sensitive.

Equal (default)

The $eq operator matches documents where the specified field is equal to the specified value.

You can’t use this operator on the $lexical field.

This is the default when you don’t specify an operator, except when you filter on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"number_of_pages": {"$eq": 714}})

print(result)

Not equal

The $ne operator matches documents where the specified field is not equal to the specified value.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"number_of_pages": {"$ne": 300}})

print(result)

Greater than

The $gt operator matches documents where the specified field is greater than the specified value.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"number_of_pages": {"$gt": 300}})

print(result)

Greater than or equal

The $gte operator matches documents where the specified field is greater than or equal to the specified value.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"number_of_pages": {"$gte": 300}})

print(result)

Less than

The $lt operator matches documents where the specified field is less than the specified value.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"number_of_pages": {"$lt": 300}})

print(result)

Less than or equal

The $lte operator matches documents where the specified field is less than or equal to the specified value.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"number_of_pages": {"$lte": 300}})

print(result)

In

The $in operator matches documents where the field contains any of the specified values. For fields that store an array, the operator matches documents where the array contains any of the specified values.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"genres": {"$in": ["Fantasy", "Romance"]}})

print(result)

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 documents where the field doesn’t contain any of the specified values.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"genres": {"$nin": ["Fantasy", "Romance"]}})

print(result)

Exists

The $exists operator matches documents that have the specified field, even if the field value is null.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"borrower": {"$exists": True}})

print(result)

All

For fields that store an array, the $all operator matches documents where the array contains all of the specified values.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"genres": {"$all": ["Fantasy", "Romance"]}})

print(result)

Size

For fields that store an array, the $size operator matches documents where the array has the specified number of elements.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"genres": {"$size": 3}})

print(result)

Not

The $not operator returns documents that don’t match the condition of the clause.

You can’t use this operator on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"$not": {"is_checked_out": False}})

print(result)

Match

Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms.

The $match operator finds documents whose $lexical field value is a lexicographical match to the specified string of space-separated keywords or terms.

You can only use the $match operator on the $lexical field. This is the only operator that the $lexical field supports.

This is the default operator when you don’t specify an operator and filter on the $lexical field.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"$lexical": {"$match": "tree hill"}})

print(result)

Combine operators (And, Or)

The $and operator joins clauses with a logical AND. Only documents that match the conditions of all clauses are returned.

The $or operator joins clauses with a logical OR. Documents 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:

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one(
    {
        "$and": [
            {
                "$or": [
                    {"is_checked_out": False},
                    {"number_of_pages": {"$lt": 300}},
                ]
            },
            {
                "$or": [
                    {"genres": {"$in": ["Fantasy", "Romance"]}},
                    {"publication_year": {"$gte": 2002}},
                ]
            },
        ]
    }
)

print(result)

To filter on a range of values, use $and with $lt/$lte and $gt/$gte:

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one(
    {
        "$and": [
            {"number_of_pages": {"$lt": 300}},
            {"number_of_pages": {"$gte": 200}},
        ]
    }
)

print(result)

Filter nested fields

Use dot notation to filter nested fields. For example, field.subfield.subsubfield.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Find a document
result = collection.find_one({"metadata.language": {"$eq": "French"}})

print(result)

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM