How to group Sanity documents by a specific field value?

6 replies
Last updated: Jan 18, 2026
Hello guys, I need a little help about how I can group a document by a specific field. For example, I have
physician
document where I have
category
string filed which can be leadership, radiologist etc. I would like a result where I can have
{
"leadership": [],
"radiologist": []
}
AI Update

Great question! To group documents by a specific field dynamically in Sanity, you'll need to use the Structure Builder API with S.documentList() and GROQ filters.

However, there's an important limitation: GROQ can't project objects with dynamic keys. So you can't get exactly the format you showed with { "leadership": [], "radiologist": [] } directly from a query.

Here are your two best options:

If you make category a reference to a separate category document type instead of just a string, you can create a dynamic structure:

*[_type == 'category'] {
  "category": name,
  "results": *[
    _type == "physician" 
    && references(^._id)
  ]
}

This gives you:

[
  {
    "category": "radiologist",
    "results": [/* physicians */]
  },
  {
    "category": "leadership", 
    "results": [/* physicians */]
  }
]

Option 2: Hardcode Categories in Your Query

If you want to keep categories as strings, you'll need to list them explicitly:

{
  "radiologist": *[_type == "physician" && category == "radiologist"],
  "leadership": *[_type == "physician" && category == "leadership"]
}

For Studio Structure Builder

You can create filtered lists in your Studio desk structure:

S.documentTypeList('category')
  .title('Posts by Category')
  .child(categoryId => 
    S.documentList()
      .title('Physicians')
      .filter('_type == "physician" && $categoryId == category')
      .params({ categoryId })
  )

The reference approach is more flexible and maintainable since it automatically adapts when you add new categories. The hardcoded approach works but requires updating your query whenever categories change.

Show original thread
6 replies

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?