Find distinct values (TypeScript)

Finds the distinct values of a key for documents in a collection.

This method finds all documents that match the filter, or all documents if no filter is applied. There can be performance, latency, and billing implications if there are many matching documents.

Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart.

Result

Returns a promise that resolves to a list of the distinct values of the specified key. Documents that do not include the requested key are ignored.

The TypeScript client will attempt to infer the return type. However, you may need to explicitly cast the return type to match the expected type.

Example resolved response:

['home_appliance', None, 'sports_equipment', {'cat_id': 54, 'cat_name': 'gardening_gear'}]

Parameters

Use the distinct method, which belongs to the Collection class.

Method signature
async distinct(
  key: Key,
  filter: CollectionFilter<Schema>,
  options: {
    timeout?: number | TimeoutDescriptor,
  },
): Flatten<(SomeDoc & ToDotNotation<Schema>)[Key]>[]
Name Type Summary

key

string

The name of the field for which to find values.

See the examples for usage.

filter

CollectionFilter<Schema>

An object that defines filter criteria using the Data API filter syntax. The method only finds documents that match the filter criteria. Filters can improve performance by reducing the number of documents that the Data API processes.

You must use & to escape any . or & in field names in the filter clause. You cannot use & to escape any other characters. For more information, see Work with . and & in field names (TypeScript).

For a list of available filter operators and more examples, see Filter operators for collections (TypeScript).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a filter.

options

object

Optional. The options for this operation. See Properties of options for more details.

Properties of options
Name Type Summary

timeout

number | TimeoutDescriptor

Optional.

The timeout(s) to apply to this method. You can specify requestTimeoutMs and generalMethodTimeoutMs. Since this method issues a single HTTP request, these timeouts are equivalent. If you specify both, the minimum of the two will be used.

For more information about the TimeoutDescriptor, see TypeScript client internals: TimeoutDescriptor. If you specify a number instead of a TimeoutDescriptor object, that number will be applied to both requestTimeoutMs and generalMethodTimeoutMs.

Examples

The following examples demonstrate how to find distinct values of a key for documents in a collection.

Find distinct values of a top level field

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");

(async function () {
  // Find distinct values
  const result = await collection.distinct("publication_year", {});

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

Find distinct values of a nested field

To find distinct values for a nested field, use dot notation. For example, field.subfield.subsubfield.

You must use & to escape any literal . or & in field names.

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");

(async function () {
  // Find distinct values
  const result = await collection.distinct("metadata.language", {});

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

Find distinct values of an index in an array

To find distinct values for a specific index in an array, specify the index with dot notation. For example, genres.0 finds distinct values of the first position of an array stored in the "genres" field.

If an array is encountered and no numeric index is specified, the method visits all items in the array.

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");

(async function () {
  // Find distinct values
  const result = await collection.distinct("genres.2", {});

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

Find distinct values for a subset of documents

You can use a filter to find distinct values across documents that match the filter.

For a list of available filter operators and more examples, see Filter operators for collections (TypeScript).

Filters can use only indexed fields. If you apply selective indexing when you create a collection, you cannot reference non-indexed fields in a 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");

(async function () {
  // Find distinct values
  const result = await collection.distinct("publication_year", {
    $and: [{ is_checked_out: false }, { number_of_pages: { $lt: 300 } }],
  });

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

Client reference

For more information, see the client reference.

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