👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Groq query issue with getting movies and character names for each person in the Sanity movies database.

3 replies
Last updated: Mar 18, 2024
Hi all.Little help with a groq query please. Using the Sanity movies database for a demo purpose. I want to get each
person
get their movies and get their characterName.
This is works except it’s for one person I need to hardcode the
_id
in the
as
bit



*[_type=='person' && _id=="person_sigourney-weaver"]{
  _id,
  "slug": slug.current,
      "image": image{
    asset->{
      url
    }
  },
  name,
  "movies": *[_type=='movie' && references(^._id)]{
    "fakeId": ^._id,
    title,
    "slug": slug.current,
    "poster": poster{
        asset->{
            url
        }
    },
   "as": castMembers[person._ref == "person_sigourney-weaver"]{
     characterName
    }
  }
}
doing this
"as": castMembers[person._ref == ^._id]{
does not work, it returns an empty array.
Where am I going wrong?

And if possible, rather than returning
as
as an array, could it come back a string?
Thank you
Mar 18, 2024, 10:16 PM
And of course I fix it seconds after I ask
"as": castMembers[person._ref == ^.^._id]{

double caret - so is that going up outside of
castMembers
and up again to the
references
?
Mar 18, 2024, 10:20 PM
Yes, it’s taking you up to the scope of the
person
projection.
Mar 18, 2024, 10:45 PM
user R
Ain't that how it always goes? 😅
Mar 18, 2024, 11:53 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?