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

5 replies
Last updated: Dec 21, 2021
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
Hey Harris! Not sure I understand your data structure correctly -- perhaps something like this?

https://groq.dev/3YiVr0FybQxaY6M9VtVwbb

{
  "open": *[
    _type == "event"
    && count(info[@.openForRegistration]) > 0
  ],
  "fixedDate": *[
    _type == "event"
    && count(info[@.hasFixedDate]) > 0
  ],
  "future": *[
    _type == "event"
    && count(info[@.eventDate > now() ]) > 0
  ],
  "past": *[
    _type == "event"
    && count(info[@.eventDate < now() ]) > 0
  ]
}
I'm not sure to clearly get your data structure but you can definitively do something like
*[_type == 'event'
  && openings[
    hasFixedDate == true
    && dateTime(eventDate) > dateTime(now())
    && openForRegistration == true
  ]
]
^ πŸ™‡β€β™‚οΈ Yeah, I haven't try it but this is likely something I need. I need to combine multiple condition like that and was not sure what the syntax would look like.Will report back later if it works. And
user G
’s snippet would also come in handy later on I'm sure. Thanks a bunch! ❀️
A little bit different question than the last. If want to to return the documents that satisfy those condition (upcoming events that has open openings), and with only the openings data that satisfy said condition, I would need to do it further down like this, right? Am I missing a more efficient way?
*[_type == 'event' && openings[
    hasFixedDate == true
    && dateTime(eventDate) > dateTime(now())
    && openForRegistration == true
  ] {
      ...,
      openings[
        hasFixedDate == true
        && dateTime(eventDate) > dateTime(now())
        && openForRegistration == true]
    }
]
You can optimize it that way
const conditions = () => `hasFixedDate == true
  && dateTime(eventDate) > dateTime(now())
  && openForRegistration == true`

const query = `*[_type == 'event' && openings[${conditions()}]]{
  ...,
  openings[${conditions()}]
}`
That's the only way right now to do it, fragment are not implemented in GROQ right now.

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?