Joint session with Vercel: How to build intelligent storefronts (May 15th)

GROQ query to find all movie screenings with a specific actor

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

By Bryan Robinson


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

Bryan Robinson

Sr. DevRel @ Algolia

United States

Visit Bryan Robinson's profile