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

10 replies
Last updated: Oct 18, 2022
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
You could do

*[_type == "movie" && _id == myID][0]
its same querying whole database and return array and we want the first object it is different by thought process i think our strategy will take more time
Not sure if I follow , but that will return the first item of the array. The object that you need
What Pedro told you is the solution.
*[…]
queries returning an array, and qualifying them with
[0]
(or any number really) ensures it returns not an array but only one entry.
user F
dont you feel the thing which we do is not optimized way to query a object where we had to query only one object just by id now we are actually appling a filter and returning an array and just the first one
You can drop the leading star then? I think this should do what you want.
It’s the way groq is built, I think it’s a fantastic way to query lots of things. If performance worries you test it out and you will see that it’s fast enough, sanity will handle for you everything and only send the desired document in the response
I agree with Kitty and Pedro. The consistency is a feature, not a bug. If
*[FILTER]
could return either an array or an object, what would you expect to return from
*[FILTER][0..2]
when there are 2 documents? What about when there’s only 1 document?
If I am not mistaken, when it comes to the star and concerns about performance, another way to think of it isn't "go retrieve everything and then pare it down to this" but instead thinking of it like a scope, or qualifying just how wide of a net to cast when implementing the filtering.
It's also a bit like a subject and a predicate. The filter is a qualifier, and therefore
can act on any starting scope, but it would need something to filter in order to be effective at being a filter.
Similarly, knowing which informational "neighborhoods" are free to venture into for retrieval helps to shape the request. If the filter said "two story houses" and your projection returned the value of the home, the data will be very different here in central Florida versus downtown Manhattan. And also would be more expansive and elaborate if the 'neighborhood' was an entire country (especially if you were using the
new math functions for arrays 😉 ).

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?