Filter query results by reference property in array

3 replies
Last updated: Jan 27, 2026
Hi, I was wondering how I could filter query results by a property that is a reference? i.e. i have X amount of “downloads” and want to filter them by a given “tag”. “tags” is an array of references.
AI Update

To filter query results by a reference property (like filtering "downloads" by a given "tag" where tags is an array of references), you'll want to use filtering within array projections using the @ operator (parent scope operator).

Here's the pattern that works:

*[_type == 'downloads']{
  'tags': tags[(@->someProperty == someValue)]->
}[length(tags) > 0]

For your specific case, if you want to filter downloads that have at least one tag matching certain criteria:

*[_type == 'downloads']{
  'tags': tags[(@->_id == "specific-tag-id")]->
}[length(tags) > 0]

The key parts here:

  1. tags[(@->someProperty == condition)] - This filters the array of references inline. The @ refers to the current item in the array, and -> dereferences it to access properties of the referenced document.

  2. [length(tags) > 0] - This final filter ensures you only get documents where at least one tag matched your criteria.

You can also add multiple conditions:

*[_type == 'downloads']{
  'matchingTags': tags[(@->active == true && @->category == "featured")]->
}[length(matchingTags) > 0]

Important: This syntax requires API version v2021-03-25 or later. Make sure your query client is configured to use this version:

const client = sanityClient({
  // ... other config
  apiVersion: '2021-03-25'
})

This approach is much more efficient than fetching all documents and filtering client-side, as the filtering happens at the query level in the Content Lake.

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