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

Filtering an array of linked events based on date in a Groq query

13 replies
Last updated: Jan 6, 2023
Hey, hoping someone could give me a hand with filtering a projection?
I have an array of linked events that need to disappear after their date has passed, and I could do this in the frontend but would quite like to do it as part of my query instead.
I know this isn't valid groq, but I essentially want to do the following (where
selected
is my array of events)
*[_type == 'page' ]{
    whatsOn {'results': selected[endDate ? (endDate >= '2022-01-06') : (date >= '2022-01-06')]->},
  } | order(_updatedAt desc)[0]
Some events have an
endDate
but others just have a
date
, then I want to only return the ones where this date is in the future. Does that make sense?
Jan 6, 2023, 9:43 AM
You can use a simple boolean expression:
(endDate != null && endDate >= '2022-01-06') || (endDate == null && date >= '2022-01-06')
But this can probably be simplified to just:

coalesce(endDate, date) >= '2022-01-06`
Jan 6, 2023, 10:02 AM
Thanks
user L
, how can I tell it not to return from the projection though?
*[_type == 'pageChristmas' ]{
    whatsOn {'results': selected[coalesce(endDate, date) >= '2022-01-06']->{title}},
  } | order(_updatedAt desc)[0]
Returns 0 results, when it should return one
Jan 6, 2023, 10:03 AM
What do you mean by “not return”? If it’s returning no results, then something is wrong with the filter expression. 🙂
I noticed you use
->
. So is
selected
actually an array of refs? If so, you need to put the arrow here instead:
selected[]->[coalesce(endDate, date) >= '2022-01-06`] { title }
          ^^
Jan 6, 2023, 10:08 AM
Sorry for not being clear, yes this is an array of references to events elsewhere on my site. As the event ends, I want it to automatically hide from the page.Unfortunately this:

*[_type == 'pageChristmas' ]{
    whatsOn {'results':selected[]->[coalesce(endDate, date) >= '2022-01-06']},
  } | order(_updatedAt desc)[0]
Just returns null for each result?
Jan 6, 2023, 10:15 AM
Can you tell me what
selected[]->
returns? With no projection or filtering.
Jan 6, 2023, 10:16 AM
Of course, so its within an object called whatsOn, which has all of this content
Jan 6, 2023, 10:19 AM
Got it, so what happens if you just do
selected[]->
(no projection or filter after)?
Jan 6, 2023, 10:26 AM
Sorry i have just realised that I put 2022 earlier, I just got the new year wrong haha, i just want them to be in the future so should be:
*[_type == 'pageChristmas' ]{
    whatsOn {'results':selected[]->[coalesce(endDate, date) >= '2023-01-06']},
  } | order(_updatedAt desc)[0]
Jan 6, 2023, 10:41 AM
and there are definetly some with a date in the future
Jan 6, 2023, 10:42 AM
Sorry, my fault. You need to wrap the array expression in parantheses:
(selected[]->)[coalesce(endDate, date) >= '2023-01-06']

Jan 6, 2023, 11:01 AM
ah perfect!! That worked!
Jan 6, 2023, 11:08 AM
Thank you so much for your help!
Jan 6, 2023, 11:08 AM
Awesome!
Jan 6, 2023, 11:08 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?