Filter operators for collections (TypeScript)

Many Data API commands, like Find documents (TypeScript), 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 (TypeScript).

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.

This is the default when you do not specify an operator.

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ number_of_pages: { $eq: 714 } });

  console.log(result);
})();

Not equal

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ number_of_pages: { $ne: 300 } });

  console.log(result);
})();

Greater than

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ number_of_pages: { $gt: 300 } });

  console.log(result);
})();

Greater than or equal

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ number_of_pages: { $gte: 300 } });

  console.log(result);
})();

Less than

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ number_of_pages: { $lt: 300 } });

  console.log(result);
})();

Less than or equal

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ number_of_pages: { $lte: 300 } });

  console.log(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.

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({
    genres: { $in: ["Fantasy", "Romance"] },
  });

  console.log(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.

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({
    genres: { $nin: ["Fantasy", "Romance"] },
  });

  console.log(result);
})();

Exists

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ borrower: { $exists: true } });

  console.log(result);
})();

All

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({
    genres: { $all: ["Fantasy", "Romance"] },
  });

  console.log(result);
})();

Size

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({ genres: { $size: 3 } });

  console.log(result);
})();

Not

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({
    $not: { is_checked_out: false },
  });

  console.log(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:

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({
    $and: [
      {
        $or: [{ is_checked_out: false }, { number_of_pages: { $lt: 300 } }],
      },
      {
        $or: [
          { genres: { $in: ["Fantasy", "Romance"] } },
          { publication_year: { $gte: 2002 } },
        ],
      },
    ],
  });

  console.log(result);
})();

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({
    $and: [
      { number_of_pages: { $lt: 300 } },
      { number_of_pages: { $gte: 200 } },
    ],
  });

  console.log(result);
})();

Filter nested fields

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

import {
  DataAPIClient,
  UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
  token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
  keyspace: "KEYSPACE_NAME",
});
const collection = database.collection("COLLECTION_NAME");

// Find a document
(async function () {
  const result = await collection.findOne({
    "metadata.language": { $eq: "French" },
  });

  console.log(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