How to get all the Type Names in a Dataset

1 replies
Last updated: Nov 19, 2021
How could I get all the type names used in my dataset? Something like "select distinct"...
AI Update

You can get all unique type names from your dataset using GROQ with a query like this:

array::unique(*[]._type)

This works by:

  1. *[] - Fetches all documents
  2. ._type - Extracts just the _type field from each document (returns an array of type names)
  3. 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 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.

in vision you can select vX as api version and then you can get all types like so:

distinct(*{ _type }._type)

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.

Was this answer helpful?