🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Limiting the scope of a filter within a Groq query to the returned scope.

20 replies
Last updated: Jan 21, 2022
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

*[_type == 'module' && slug.current == $module]{
  "chapters": chapters[]->{...}
}
I want to match against slug.current inside the returned
chapters
Jan 20, 2022, 4:11 PM
I wonder if something like this could work?
*[_type == 'module' && slug.current == $module]{
  "chapters": chapters[@->slug.current == "something"]->{...}
}
Jan 20, 2022, 4:19 PM
Yes!! thank you
user G
Jan 20, 2022, 4:21 PM
I could not for the life of me understand how to filter array results using groq
Jan 20, 2022, 4:22 PM
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
Super simple compared to what I was doing before (and what wasn’t working)
Jan 20, 2022, 4:23 PM
Nice, glad you got it! Yeah GROQ is pretty awesome
Jan 20, 2022, 4:24 PM
It really is
Jan 20, 2022, 4:24 PM
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
I wonder if something like this could work?
*[_type == 'module' && slug.current == $module]{
  "chapters": chapters[@->slug.current == "something"]->{...}
}
Jan 20, 2022, 4:19 PM
I haven't checked, but maybe something like this?
chapters[@->slug.current == $chapter][0]->
I'm not sure if deref would work in this case
Jan 20, 2022, 4:26 PM
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
I guess what I want is to drop the array in the projection
Jan 20, 2022, 4:27 PM
Not critical though, at least this gets me a result 🙂
Jan 20, 2022, 4:28 PM
Ah I see.. I think you can always bring in the big boy
*[]
to get you exactly what you want:
*[_type == 'module' && slug.current == $module]{
  "chapter": *[_type == 'chapter' && _id in ^.chapters[]._ref && slug.current == $chapter][0]
}
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
Jan 20, 2022, 4:30 PM
Actually your first example did work… not sure what I did the first time that didn’t
Jan 20, 2022, 4:31 PM
*[_type == 'module' && slug.current == $module][0]{
  "chapter": chapters[@->slug.current == $chapter][0]->{
    "section": section[@->slug.current == $section][0]->{ ... }
  }
}
Jan 20, 2022, 4:31 PM
Thanks for your help User!
Jan 20, 2022, 4:32 PM
oh sweet, thanks for letting me know User! 🙌
Jan 20, 2022, 4:32 PM
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
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.

Was this answer helpful?