Structured Content 2022: Join our conference to explore fresh perspectives on content and digital experiences →

GROQ query to find all movie screenings with a specific actor

By Bryan Robinson

Find new connections in a dataset by filtering a second time on dereferenced data

screeningsByActor.js

*[_type == 'screening']{
  title,
  movie->
}[$actorId in movie.castMembers[].person._ref]

This GROQ snippet takes an $actorId and returns screening documents that have a reference to a movie with this actor. This is using the default movie schema and data that you can get by running sanity init in the Sanity CLI.

The query itself comes in three parts:

  1. *[_type == 'screening']: Find all documents with a type of screening
  2. Run a projection ({}) to get the screening title (and any other information), and dereference the movie information: movie->
  3. [$actorId in movie.castMembers[].person._ref]: Run a filter on the returned array to check if the actor's document ID is in the castMember array

This is an example of using GROQ for equijoins.

Contributor

Other schemas by author