Write a GROQ query for getting all documents that has an ID in an array of IDs

4 replies
Last updated: Nov 29, 2025
I'm trying to write a GROQ query for getting all documents that has an ID in an array of IDs
I.e. - I have a list of IDs called
favorites
, which look like
['asdf', '1234', 'abcd']
. Each of these are document IDs.
How do I write a GROQ query that only gets the documents with IDs in that list?
AI Update

You can use the in operator to filter documents whose _id is in your array of IDs. Here's how:

*[_id in ['asdf', '1234', 'abcd']]

Or if you have your favorites array as a variable:

*[_id in $favorites]

The in operator checks if the value on the left side exists anywhere in the array on the right side. So _id in ['asdf', '1234', 'abcd'] will return true for any document whose _id matches one of those values.

You can also combine this with other filters and projections:

*[_id in $favorites]{
  _id,
  title,
  // ... other fields you want
}

This is exactly the pattern shown in the GROQ Query Cheat Sheet where it demonstrates filtering with in:

*[_type in ["movie", "person"]] // _type is movie or person
*[title in ["Aliens", "Interstellar", "Passengers"]]

The same syntax works for any field, including _id!

Show original thread
4 replies

Sanity – Build the way you think, not the way your CMS thinks

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.

Was this answer helpful?