Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Querying a single object by ID in Sanity.io and the optimization concerns around it.

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
Oct 16, 2022, 5:56 AM
You could do

*[_type == "movie" && _id == myID][0]
Oct 16, 2022, 8:12 AM
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
Oct 16, 2022, 8:35 AM
Not sure if I follow , but that will return the first item of the array. The object that you need
Oct 16, 2022, 10:47 AM
What Pedro told you is the solution.
Oct 16, 2022, 2:43 PM
*[…]
queries returning an array, and qualifying them with
[0]
(or any number really) ensures it returns not an array but only one entry.
Oct 16, 2022, 2:44 PM
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
Oct 16, 2022, 4:23 PM
You can drop the leading star then? I think this should do what you want.
Oct 16, 2022, 4:32 PM
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
Oct 16, 2022, 6:20 PM
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?
Oct 17, 2022, 4:50 PM
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 😉 ).
Oct 18, 2022, 1:13 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?