GROQ query: Filter by nested array object type and array contains value
The issue you're encountering is a common GROQ pitfall when working with nested arrays. When you use array[] syntax multiple times in a filter, the operators don't associate the way you might expect - they expand to flat arrays and compare arrays to values, which doesn't work.
Your query:
*[
_type == "client"
&& provider[].carrierId == 5
&& activation[]._type == "busmaticScheduler"
&& "upis-city" in activation[].plugins
]The problem is that provider[].carrierId == 5 becomes [1, 2, 5, ...] == 5 (comparing an array to a value), and "upis-city" in activation[].plugins tries to check if the string exists in a flattened array of all plugins from all activation items.
The solution is to use count() with filtered arrays:
*[
_type == "client"
&& count(provider[carrierId == 5]) > 0
&& count(activation[_type == "busmaticScheduler" && "upis-city" in plugins]) > 0
]This works because:
provider[carrierId == 5]filters the provider array to only items where carrierId equals 5count(...)checks if any items match (count > 0)- For the nested condition, you can combine both checks in a single filter:
activation[_type == "busmaticScheduler" && "upis-city" in plugins]
As explained in this community answer about querying nested arrays, the key insight is that array filters with conditions inside brackets return only matching elements, while the projection syntax with [] flattens everything.
If you also want to clean up your projection to only return the matching items, you can do:
*[
_type == "client"
&& count(provider[carrierId == 5]) > 0
&& count(activation[_type == "busmaticScheduler" && "upis-city" in plugins]) > 0
]{
...,
"provider": provider[carrierId == 5],
"activation": activation[_type == "busmaticScheduler" && "upis-city" in plugins]
}This way your results only show the provider and activation items that actually matched your filter criteria, making it easier to see why each document was returned - similar to the pattern shown in the nested arrays example where they filtered down to only relevant pricing plans within variants.
Show original thread8 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.