Filtering by array references in Sanity.io
20 replies
Last updated: Apr 7, 2022
I
Is there a way to filter by elements of an array of references? I have a ProductListing type which references many Products. I want to get all listings that have at least one product in stock. I can’t seem to be able to figure this one out.
I tried a few options along these lines, but it doesn’t seem to work:
I was looking for something like a
I tried a few options along these lines, but it doesn’t seem to work:
*[_type=='ProductListing' && items[]->stock > 0]{ _id }
sumfunction or any way to filter items without doing a select and filtering client side.I will have more conditions as well based on whether items are currently disabled and such.
Apr 6, 2022, 5:55 PM
I
Yes, it’s an array of references
Apr 6, 2022, 6:06 PM
I
[ { "items": [ 10000000, 0, 999998, 0, 11 ] } ]
Apr 6, 2022, 6:07 PM
Thanks for confirming. Could you try this?
*[_type=='ProductListing' && count(items[]->stock) > 0]
Apr 6, 2022, 6:07 PM
Thanks for confirming. Could you try this?
*[_type=='ProductListing' && count(items[]->stock) > 0]
Apr 6, 2022, 6:08 PM
I
*[_type=='ProductListing' && count(items[]->stock) > 0]{ 'items': items[]->stock, }[0..1]
Apr 6, 2022, 6:08 PM
I
[ {
“items”: [
10000000,
0,
999998,
0,
11
]
},
{
“items”: [
999
]
}
]
“items”: [
10000000,
0,
999998,
0,
11
]
},
{
“items”: [
999
]
}
]
Apr 6, 2022, 6:08 PM
I
Hm, it could actually be working
Apr 6, 2022, 6:08 PM
I
let me look through a larger set
Apr 6, 2022, 6:08 PM
I
{ “items”: [
0
]
}
0
]
}
Apr 6, 2022, 6:09 PM
I
Found this
Apr 6, 2022, 6:09 PM
I
I will let it sit till the morning. Let me know if you have ideas on filtering through references on one or more conditions. Perhaps a
scorecould work somehow
Apr 6, 2022, 6:11 PM
Yeah, that was bad advice on my part, sorry. I think you may want a join. Just going to test something locally to get the code right.
Apr 6, 2022, 6:11 PM
Something like:
*[_type=='ProductListing']{ 'items': items[]->stock, }[items[]->stock != 0]
Apr 6, 2022, 6:12 PM
I
{ “items”: [
0
]
}
0
]
}
Apr 6, 2022, 6:12 PM
I
Still a few
Apr 6, 2022, 6:12 PM
I
Is there a way to intersect two arrays? let’s say I would do something like:
items[]._ref in *[filter]._id
Apr 6, 2022, 6:16 PM
Okay, apologies again! I think this may finally capture it:
This requires
*[_type == 'ProductListing']{ 'items': items[(@->stock != 0)]->stock, _id }[length(items) > 0]
v2021-03-25or later of the API.
Apr 6, 2022, 6:32 PM
I
Wow. Works like magic. I was able to expand on the filter further with additional conditions! Long live GROQ
Apr 7, 2022, 6:58 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.