Find distinct values
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. |
Result
-
Python
-
TypeScript
-
Java
-
curl
Returns a list of the distinct values of the specified key. The method excludes documents that do not include the requested key.
Example response:
['home_appliance', None, 'sports_equipment', {'cat_id': 54, 'cat_name': 'gardening_gear'}]
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'}]
Returns a list of the distinct values of the specified key (DistinctIterable<F>
).
Documents that do not include the requested key are ignored.
This method has no literal equivalent in HTTP.
Instead, you can use Find documents, and then use jq
or another utility to extract the desired values from the response.
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Name | Type | Summary |
---|---|---|
|
|
The name of the field whose value is inspected across documents. Keys can use dot-notation to descend to deeper document levels. Example of acceptable |
|
|
A predicate expressed as a dictionary according to the Data API filter syntax. Examples are |
|
|
A timeout, in milliseconds, for the operation. This method uses the collection-level timeout by default. |
Name | Type | Summary |
---|---|---|
|
|
The name of the field whose value is inspected across documents. Keys can use dot-notation to
descend to deeper document levels. Example of acceptable key values: |
|
A filter to select the documents to use. If not provided, all documents will be used. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort or filter queries. |
Name | Type | Summary |
---|---|---|
|
|
The name of the field on which project the value. |
|
Criteria list to filter the document. The filter is a JSON object that can contain any valid Data API filter expression. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in sort or filter queries. |
|
|
|
The type of the field we are working on |
This method has no literal equivalent in HTTP.
Instead, you can use Find documents, and then use jq
or another utility to extract the desired values from the response.
Examples
The following examples demonstrate how to find documents in a collection.
Find distinct values of a top level property
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Find distinct values
result = collection.distinct("publicationYear")
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
(async function () {
// Find distinct values
const result = await collection.distinct("publicationYear");
console.log(result)
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.client.model.DistinctIterable;
public class FindDistinct {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Find distinct values
DistinctIterable<Document, String> result = collection
.distinct("publicationYear", String.class);
for (String fieldValue : result) {
System.out.println(fieldValue);
}
}
}
This method has no literal equivalent in HTTP.
Instead, you can use Find documents, and then use jq
or another utility to extract the desired values from the response.
Find distinct values of a nested property
To find distinct values for a nested property, use dot notation.
For example, field.subfield.subsubfield
.
To use dot notation for a list, specify a numeric index.
For example, field.3
.
If a list is encountered and no numeric index is specified, the method visits all items in the list.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Find distinct values
result = collection.distinct("metadata.language")
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
(async function () {
// Find distinct values
const result = await collection.distinct("metadata.language");
console.log(result)
})();
The Java client does not support dot notation to find distinct values for nested fields.
This method has no literal equivalent in HTTP.
Instead, you can use Find documents, and then use jq
or another utility to extract the desired values from the response.
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 Data API operators.
Filters can use only indexed fields. If you apply selective indexing when you create a collection, you can’t reference non-indexed fields in a filter.
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
# Get an existing collection
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")
# Find distinct values
result = collection.distinct(
"publicationYear",
filter={
"$and": [
{"isCheckedOut": False},
{"numberOfPages": {"$lt": 300}},
]
}
)
print(result)
import { DataAPIClient } from '@datastax/astra-db-ts';
// Get an existing collection
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const collection = database.collection('COLLECTION_NAME');
(async function () {
// Find distinct values
const result = await collection.distinct(
"publicationYear",
{
"$and": [
{"isCheckedOut": false},
{"numberOfPages": {"$lt": 300}},
]
}
);
console.log(result)
})();
package com.datastax.astra.client.collection;
import com.datastax.astra.client.Collection;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.model.Document;
import com.datastax.astra.client.model.Filter;
import com.datastax.astra.client.model.Filters;
import com.datastax.astra.client.model.DistinctIterable;
public class FindDistinct {
public static void main(String[] args) {
// Get an existing collection
Collection<Document> collection = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
.getDatabase("ASTRA_DB_API_ENDPOINT")
.getCollection("COLLECTION_NAME");
// Find distinct values
Filter filter = Filters.and(
Filters.eq("isCheckedOut", false),
Filters.lt("numberOfPages", 300));
DistinctIterable<Document, String> result = collection
.distinct("publicationYear", filter, String.class);
for (String fieldValue : result) {
System.out.println(fieldValue);
}
}
}
This method has no literal equivalent in HTTP.
Instead, you can use Find documents, and then use jq
or another utility to extract the desired values from the response.
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.