Limiting the scope of a filter within a Groq query to the returned scope.
20 replies
Last updated: Jan 21, 2022
M
Is there a way to limit the scope of a filter within a groq query to the returned scope? Every query seems to be global (*[]), but I want to search within the context of a returned array of objects
I want to match against slug.current inside the returned
*[_type == 'module' && slug.current == $module]{ "chapters": chapters[]->{...} }
chapters
Jan 20, 2022, 4:11 PM
D
I wonder if something like this could work?
*[_type == 'module' && slug.current == $module]{ "chapters": chapters[@->slug.current == "something"]->{...} }
Jan 20, 2022, 4:19 PM
M
Yes!! thank you
user G
Jan 20, 2022, 4:21 PM
M
I could not for the life of me understand how to filter array results using groq
Jan 20, 2022, 4:22 PM
M
The full working query
*[_type == 'module' && slug.current == $module]{ "chapter": chapters[@->slug.current == $chapter]->{ "section": section[@->slug.current == $section]-> {...} } }
Jan 20, 2022, 4:23 PM
M
Super simple compared to what I was doing before (and what wasn’t working)
Jan 20, 2022, 4:23 PM
D
Nice, glad you got it! Yeah GROQ is pretty awesome
Jan 20, 2022, 4:24 PM
M
It really is
Jan 20, 2022, 4:24 PM
M
I’m curious, since there is only one result, but it’s in an array, can the projection drop the array and only return the object
Jan 20, 2022, 4:25 PM
D
I wonder if something like this could work?
*[_type == 'module' && slug.current == $module]{ "chapters": chapters[@->slug.current == "something"]->{...} }
Jan 20, 2022, 4:19 PM
D
I haven't checked, but maybe something like this?
I'm not sure if deref would work in this case
chapters[@->slug.current == $chapter][0]->
Jan 20, 2022, 4:26 PM
M
That one doesn’t work as it ends up only looking at the first item it’s trying to match against
Jan 20, 2022, 4:26 PM
M
I guess what I want is to drop the array in the projection
Jan 20, 2022, 4:27 PM
M
Not critical though, at least this gets me a result 🙂
Jan 20, 2022, 4:28 PM
D
Ah I see.. I think you can always bring in the big boy
But it may make the query a bit harder to read, I wonder if there's an easier way to slice & deref an array of ref at the same time
*[]to get you exactly what you want:
*[_type == 'module' && slug.current == $module]{ "chapter": *[_type == 'chapter' && _id in ^.chapters[]._ref && slug.current == $chapter][0] }
Jan 20, 2022, 4:30 PM
M
Actually your first example did work… not sure what I did the first time that didn’t
Jan 20, 2022, 4:31 PM
M
*[_type == 'module' && slug.current == $module][0]{ "chapter": chapters[@->slug.current == $chapter][0]->{ "section": section[@->slug.current == $section][0]->{ ... } } }
Jan 20, 2022, 4:31 PM
M
Thanks for your help User!
Jan 20, 2022, 4:32 PM
D
oh sweet, thanks for letting me know User! 🙌
Jan 20, 2022, 4:32 PM
V
I am late to the party and I may have misunderstood but this is what I arrived at (it's clumsy and maybe off of the use case but I learned something haha )
Jan 20, 2022, 4:48 PM
A
Love the GROQ expertise in this thread! 🙂
Jan 21, 2022, 12:09 PM
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.