
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThe issue you're encountering is about scope levels with the parent operator (^). When you're inside the castMembers array filter, you need to reference the person's _id from two levels up, not just one.
Here's what's happening:
^ = the movie document^ (^.^) = the person document (what you actually need)So your query should use ^.^._id instead of ^._id:
*[_type=='person']{
_id,
"slug": slug.current,
"image": image{
asset->{
url
}
},
name,
"movies": *[_type=='movie' && references(^._id)]{
title,
"slug": slug.current,
"poster": poster{
asset->{
url
}
},
"as": castMembers[person._ref == ^.^._id]{
characterName
}
}
}The scope breakdown:
^._id now refers to the movie)castMembers array filter (you need ^.^._id to get back to the person)As explained in the GROQ parent operator documentation, you can chain multiple parent operators to traverse multiple levels up in scope.
For returning a string instead of an array, you can use the string::join() function:
"as": string::join(castMembers[person._ref == ^.^._id].characterName, ", ")This will take all the characterName values and join them into a single comma-separated string. If there's only one character, it'll just return that string. If someone played multiple characters in the same movie, they'll all be listed together.
The complete query would be:
*[_type=='person']{
_id,
"slug": slug.current,
"image": image{
asset->{
url
}
},
name,
"movies": *[_type=='movie' && references(^._id)]{
title,
"slug": slug.current,
"poster": poster{
asset->{
url
}
},
"as": string::join(castMembers[person._ref == ^.^._id].characterName, ", ")
}
}Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store