Query issue with returning a specific "collection" document in a Slack thread.
5 replies
Last updated: May 11, 2020
B
Hi, I am trying to understand why the "collection" in the below query is never returned (using -> and references(^._id) in the same filter doesn't work). Everything is linked/referenced correctly (the collection has a reference to the partner and it also references the post).
Where as this query always returns a "collection":
As does this query:
Basically the point of the query is given three slugs, I want to bring back all the documents for all three AND ensure the documents reference each other correctly (the collection references both the post and the partner, the post references the partner).
Anyway, any advice would be greatly appreciated!
*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] { "post": @{ ..., images[]{..., asset->{...}} }, "collection": *[_type == 'collection' && slug.current == $collectionSlug && partner->slug.current == $partnerSlug && references(^._id) ]{ ..., posts[]->{ ..., images[]{..., asset->{...}} }, }[0], "partner": *[_type == 'partner' && slug.current == $partnerSlug]{ ..., logo{..., asset->{...}} }[0] }
*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] { "post": @{ ..., images[]{..., asset->{...}} }, "collection": *[_type == 'collection' && slug.current == $collectionSlug && references(^._id) ]{ ..., posts[]->{ ..., images[]{..., asset->{...}} }, }[0], "partner": *[_type == 'partner' && slug.current == $partnerSlug]{ ..., logo{..., asset->{...}} }[0] }
*[_type == 'post' && slug.current == $postSlug && partner->slug.current == $partnerSlug] { "post": @{ ..., images[]{..., asset->{...}} }, "collection": *[_type == 'collection' && slug.current == $collectionSlug && partner->slug.current == $partnerSlug ]{ ..., posts[]->{ ..., images[]{..., asset->{...}} }, }[0], "partner": *[_type == 'partner' && slug.current == $partnerSlug]{ ..., logo{..., asset->{...}} }[0] }
Anyway, any advice would be greatly appreciated!
May 11, 2020, 1:48 AM
M
Hi Matthew, without having reproduced this yet, have you tried using
references(…)for both the
_idand
$partnerSlug? Also, you have verified that the returned collection is exactly the same for both working queries, correct?
May 11, 2020, 11:59 AM
P
Hi Matthew, without having reproduced this yet, have you tried using
references(…)for both the
_idand
$partnerSlug? Also, you have verified that the returned collection is exactly the same for both working queries, correct?
May 11, 2020, 11:59 AM
M
Hi Peter, I thought references could only be used with an _id? The partnerSlug is of type slug... And yes, the returned collection is exactly the same. I'm happy to give access to our dataset - there isn't much in there (yet).
May 11, 2020, 12:34 PM
P
Feel free to DM me and I’ll check it out 🙂
May 11, 2020, 12:39 PM
P
Resolved with the snippet below, although some follow-up is required to investigate a non-working
^._idin the query above (and variations of it).
... "collection": *[_type == 'collection' && slug.current == $collectionSlug && partner->slug.current == $partnerSlug && $postSlug in posts[]->slug.current ]{ ...
May 11, 2020, 3:36 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.