๐Ÿ”ฎ Sanity Create is here. Writing is reinvented. Try now, no developer setup

Understanding Groq queries in Sanity.io Slack thread

14 replies
Last updated: Feb 26, 2021
hey guys! i have a groq question... is this possible? `
*[_type == 'collection' && *[items && slug.current == 'look-1']]

is an array of objects under a
.. i want to get the collection that has that
of a particular slug ๐Ÿ™
Feb 25, 2021, 5:57 PM
*[_type == 'collection' && 'look-1' in items[].slug.current]
I think that does what you're looking for, assuming that
is a property of each
Feb 25, 2021, 6:18 PM
That query is essentially doing an array map on
to make an array of all the slugs, and then checking if any of them match 'look-1'
Feb 25, 2021, 6:19 PM
will the
operator also work? context:
[...*[_type == 'collection'].items] {
        'collectionSlug': ^.slug.current,
        'itemSlug': slug.current,
in getStaticPaths for /[collection]/[item]
Feb 25, 2021, 6:20 PM
nope ๐Ÿค” i need to read up more on this ๐Ÿ˜„
Feb 25, 2021, 6:22 PM
[...*[_type == 'collection'].items]
doesnt work... how do i flatten all items of all collections ?
Feb 25, 2021, 6:38 PM
Feb 25, 2021, 7:21 PM
If you want to get all collections which have a property
which contains a slug, then I'm pretty sure I was on the right track. It sounds like you want to do something different though?
Feb 25, 2021, 8:36 PM
If you are trying to bring back a particular
in the
array within your
results, based on a slug you provide, then you could actually do that within the projection:
*[_type == 'collection']{
   "targetItem": items[slug.current == $slug]
Would that not work for you? I'm not 100% sure what you are trying to do, so I'm kind of taking a shot and seeing if I get it right here
Feb 25, 2021, 8:40 PM
Feb 25, 2021, 8:42 PM
i think that's it! didn't know the syntax
items[conditional here]
Feb 25, 2021, 8:43 PM
will give it a go, thank you djeglin
Feb 25, 2021, 8:43 PM
Yeah... So, because projections are just groq statements in and of themselves, you can use the filter (like you do to start your query) inside it. You're replacing the
(which is the array to filter) with
here, so the filter operates against that child array of the item being projected. I'm using it in a couple of places in the project I'm working on at the moment
Feb 25, 2021, 8:45 PM
thank you so much
user M
! very greatful for spending the time to explain it to me too ๐Ÿ™‚ i learned something new!
Feb 26, 2021, 8:02 AM
No problem! I ask questions all the time... The least I can do is try to help others where I can in return ๐Ÿ˜ƒ
Feb 26, 2021, 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?