How to query single object by ID without array response in Sanity?

10 replies
Last updated: Jan 28, 2026
help how to query only single object with the help of id, i know i can do that with

*[_type == "movie" && _id === myID]
BUT IN RESPONSE WE WILL GET THE ARRAY I DONT WANT ARRAY AS RESPONSE
IS THERE ANY WAY I JUST QUERY A SIMPLE OBJECT
AI Update

Based on the Sanity documentation, yes, there's a simple way to get a single object instead of an array!

Use Array Index [0]

When you add [0] to your query, GROQ returns a single object instead of an array:

*[_type == "movie" && _id == myID][0]

This returns an object (not an array with one item).

How It Works

The slice operation [0] extracts the first element from the result array. Since you're filtering by _id (which is unique), you'll only have one result anyway, so [0] effectively unwraps it from the array.

Examples

// Returns an array with one movie
*[_type == "movie" && title == "Alien"]

// Returns a single movie object
*[_type == "movie" && title == "Alien"][0]

// Your specific case - returns single object
*[_type == "movie" && _id == myID][0]

Important Note

If no document matches your filter, [0] will return null rather than an empty array []. This is actually useful for checking if a document exists:

const movie = await client.fetch('*[_id == $id][0]', { id: myID })
if (!movie) {
  // Document doesn't exist
}

This is the standard pattern in GROQ for fetching a single document by ID or any unique identifier!

Show original thread
10 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?