
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeBased on the Sanity documentation, yes, there's a simple way to get a single object instead of an array!
[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).
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.
// 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]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!
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