Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Querying documents based on the fine-grained content of an array field inside.

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.
Dec 21, 2021, 7:11 AM
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
  ]
}
Dec 21, 2021, 7:30 AM
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
  ]
]
Dec 21, 2021, 7:31 AM
^ šŸ™‡ā€ā™‚ļø 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! ā¤ļø
Dec 21, 2021, 8:06 AM
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]
    }
]
Dec 21, 2021, 8:25 AM
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.
Dec 21, 2021, 8:29 AM

Sanityā€“ build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?