Filter array field in Sanity query to show only specific values

5 replies
Last updated: Nov 29, 2025
Hi all, how can I filter out an array returned from a query?I have a query similar to this one

*[_type == 'movie']{..., actors}
where actors is an array of objects containing the role (
leading
,
support
, etc) and the reference to the actor. I need to filter out the actors array to only include ones in a leading role.
AI Update

You can filter arrays directly within a GROQ projection using bracket notation with a filter condition. For your case with actors and roles, here's how to do it:

*[_type == 'movie']{
  ..., 
  "leadingActors": actors[role == "leading"]
}

The key is placing the filter condition [role == "leading"] directly on the array within your projection. This filters the actors array to only include objects where role equals "leading".

Here are more examples showing different filtering scenarios:

// Filter and project specific fields from the filtered array
*[_type == 'movie']{
  title,
  "leadingActors": actors[role == "leading"]{
    name,
    role
  }
}

// Filter with multiple conditions
*[_type == 'movie']{
  "topActors": actors[role == "leading" && experience > 10]
}

// Filter and follow references in the array
*[_type == 'movie']{
  "leadActors": actors[role == "leading"].person->{
    name,
    bio
  }
}

// Using match for text filtering
*[_type == 'movie']{
  castMembers[characterName match 'Ripley']{
    characterName, 
    person
  }
}

This is documented in the GROQ Query Cheat Sheet under "Object Projections" - specifically the "Filter embedded objects" example. The filter is applied during projection, so it's efficient and only returns the subset of array elements that match your condition.

Show original thread
5 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?