Query documents by array field conditions (date, boolean filters)
Yes! GROQ absolutely supports fine-grained filtering of array elements based on their content. You can query documents based on conditions within array items, which is exactly what you need.
For your use case with repeatable events, here's how to tie it all together:
*[
_type == "event" &&
hasFixedDate == true &&
count(openingInfo[
eventDate > now() &&
openForRegistration == true
]) > 0
]Let me break down what's happening here:
Array Filtering with []: The openingInfo[...] syntax filters the array, returning only elements that match the conditions inside the brackets. This is GROQ's array filter operator.
Multiple Conditions: Inside the array filter, you can combine conditions with && (AND) or || (OR) operators. Each condition is evaluated in the scope of each array element.
Checking for Matches: The count() function counts how many array elements match your conditions. By checking count(...) > 0, you ensure the document has at least one matching opening.
Here's a more complete example that also returns the matching openings:
*[
_type == "event" &&
hasFixedDate == true &&
count(openingInfo[eventDate > now() && openForRegistration == true]) > 0
] {
title,
"upcomingOpenings": openingInfo[
eventDate > now() &&
openForRegistration == true
]
}Key Points:
- The filter expression inside
[]is evaluated in the scope of each array element - You can use
@to refer to the current array element if needed - Array filters return a new array with only the matching elements
- You can chain multiple filters and projections together
The GROQ operators documentation has more details on the array filter operator if you need additional examples.
Show original thread5 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.