Groq query to exclude items from an array within a document type in Sanity
15 replies
Last updated: Jan 30, 2023
E
hey, hoping someone can give me a hand with something that i feel should be really simple but I just cant get it to work!I have a type called 'press' and then a type called 'pressFeatured' which has an array within it of references to press items.
I want to do a groq query to get back all the press items not including the ones in this featured array. I have tried this:
I want to do a groq query to get back all the press items not including the ones in this featured array. I have tried this:
*[_type == 'press' && references(*[_type == 'pressFeatured' && featured[]._ref != ^.id])]but it just returns all of the press articles, including the featured one! Anyone got any ideas?
Jan 25, 2023, 11:34 AM
A
I think you want
!(^.id in featured[].ref])
Jan 25, 2023, 12:56 PM
E
Hey
user L
, thanks for getting back to me! I've just tried *[_type == 'press' && references(*[_type == 'pressFeatured' && !(^.id in featured[].ref)])]but it returns no results? Am I missing something?
Jan 25, 2023, 1:06 PM
A
Hm, I had a typo, you need
_ref. But that might not be why.
Jan 25, 2023, 2:00 PM
E
ah sorry, I completely missed that š¤¦
*[_type == 'press' && references(*[_type == 'pressFeatured' && !(^._id in featured[]._ref)])]added the underscores to if and ref, but still no results when I'm expecting 154
Jan 25, 2023, 3:17 PM
A
Hm, Iām not sure, then. If you give me your project ID and dataset name, I can test myself.
Jan 25, 2023, 3:58 PM
E
Course, I'll dm you
Jan 25, 2023, 4:09 PM
A
Do you have an example of a document of type
pressthat references a
pressFeatureddocument?
Jan 25, 2023, 8:36 PM
E
No sorry its the other way round, so pressFeatured references press documents
Jan 26, 2023, 2:18 PM
A
references()works on the document you are filtering. So your query above means āwhere type is āpressā and it references some other documentsā.
Jan 26, 2023, 2:20 PM
A
Iām not sure what you want, then, given your correction. What do this mean:
I want to do a groq query to get back all the press items not including the ones in this featured array.
Jan 26, 2023, 2:21 PM
A
Is there more than one
pressFeatureddocument?
Jan 26, 2023, 2:21 PM
E
Sorry, I must of explained it badly! So I am trying to get back an array of
_type == 'press'without any that are listed as featured in the document
pressFeatured.There is only one document called
pressFeaturedwith the array of featured items within it, and there will only ever be 1 of these documents.
Jan 26, 2023, 2:33 PM
A
Ok, then I think you want to something like:
*[_type == 'press' && _id not in *[_type == 'pressFeatured'][0].featured[]._ref]
Jan 26, 2023, 2:34 PM
E
That gave me this error, but I changed it round to be
*[_type == 'press' && !(_id in *[_type == 'pressFeatured'][0].featured[]._ref)]and it worked perfectly! thank you!
Jan 30, 2023, 11:01 AM
A
Dāoh, Iāve been doing too much SQL lately. Yes, thatās it. š
Jan 30, 2023, 1:07 PM
Sanityā build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.