Filtering an array of linked events based on date in a Groq query
13 replies
Last updated: Jan 6, 2023
E
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
Some events have an
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
selectedis my array of events)
*[_type == 'page' ]{ whatsOn {'results': selected[endDate ? (endDate >= '2022-01-06') : (date >= '2022-01-06')]->}, } | order(_updatedAt desc)[0]
endDatebut 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
A
You can use a simple boolean expression:
But this can probably be simplified to just:
(endDate != null && endDate >= '2022-01-06') || (endDate == null && date >= '2022-01-06')
coalesce(endDate, date) >= '2022-01-06`
Jan 6, 2023, 10:02 AM
E
Thanks
Returns 0 results, when it should return one
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]
Jan 6, 2023, 10:03 AM
A
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
I noticed you use
->. So is
selectedactually 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
E
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:
Just returns null for each result?
*[_type == 'pageChristmas' ]{ whatsOn {'results':selected[]->[coalesce(endDate, date) >= '2022-01-06']}, } | order(_updatedAt desc)[0]
Jan 6, 2023, 10:15 AM
A
Can you tell me what
selected[]->returns? With no projection or filtering.
Jan 6, 2023, 10:16 AM
E
Of course, so its within an object called whatsOn, which has all of this content
Jan 6, 2023, 10:19 AM
A
Got it, so what happens if you just do
selected[]->(no projection or filter after)?
Jan 6, 2023, 10:26 AM
E
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
E
and there are definetly some with a date in the future
Jan 6, 2023, 10:42 AM
A
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
E
ah perfect!! That worked!
Jan 6, 2023, 11:08 AM
E
Thank you so much for your help!
Jan 6, 2023, 11:08 AM
A
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.