How to get all the Type Names in a Dataset
You can get all unique type names from your dataset using GROQ with a query like this:
array::unique(*[]._type)This works by:
*[]- Fetches all documents._type- Extracts just the_typefield from each document (returns an array of type names)array::unique()- Removes duplicates from the array
This is essentially the GROQ equivalent of SQL's "SELECT DISTINCT". The result will be an array of all unique document type names in your dataset.
Alternative approaches:
If you want more control or additional information, you could also use:
// Get types with document counts
{
"types": array::unique(*[]._type)
}Or if you want to see the types in a more structured way with counts:
*[] {
"type": _type
} | order(type) | {"types": array::unique(@.type)}You can test these queries in:
- The Vision plugin in your Studio
- The groq.dev playground
- Your project's HTTP API query endpoint
The array::unique() function is specifically designed for this kind of operation and is the most straightforward way to get distinct values in GROQ, similar to SQL's SELECT DISTINCT.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.