🗓️ Everything *[NYC] is back. A free gathering for AI builders. Sept 9

How to get all distinct document type names in Sanity dataset?

1 replyLast updated: Nov 29, 2025

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:

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.

Show original thread
1 reply

Was this answer helpful?

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.

Related contributions