Query documents by array field conditions (date, boolean filters)

5 replies
Last updated: Jan 28, 2026
Hi folks πŸ‘‹Is it possible to query a documents based on the fine-grained content of an array field inside?

For context, I have a repeatable event type, with an array for multiple opening info. Inside that array, there's the event date, the registration link and whether it is still open for registration for that date.
What I need is to query all the event that will: 1. Has fixed opening date, hasFixedDate == true; 2. Happen in the future, eventDate > today; 3. Still open for registration, openForRegistration == true.
I'm quite not sure how to tie it together though.
AI Update

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 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?