Trouble querying for types and limitations of querying objects in Sanity

11 replies
Last updated: May 3, 2021
Hi all!I'm having trouble querying for types. This produces no matches. However, I'm successfully using the type in an array for a page. And the returned json data is correctly displaying, and even shows the testimonial type!

I'm using the Vision query sandbox for production.


*[_type == 'testimonial']
May 1, 2021, 2:23 AM
Hi User. When you’re viewing a testimonial document in the studio, you can click the three vertical dots in the top-right corner and select Inspect. Do you then see something like:

{
  "_createdAt": "2021-03-09T06:14:07Z",
  "_id": "22c6c3cb-9f8a-4d6e-9084-627b18a6d45f",
  "_rev": "btIujVUaQMY6qaWz2C2Fbk",
  "_type": "testimonial",
  ...
May 1, 2021, 2:28 AM
Hi User, appreciate the quick response! I see this:
The type is displaying
May 1, 2021, 2:33 AM
Hi User, appreciate the quick response! I see this:
The type is displaying
May 1, 2021, 2:33 AM
It's also an object, not a document. Not sure if that makes a difference. The testimonial type is embedded in a document, and it's quite deeply nested.
May 1, 2021, 2:34 AM
Unfortunately, you won’t be able to search for an object by its type in the same way you would a document. I can’t come up with anything more eloquent than:

*[length(content[_type == 'testimonial']) > 0].content[_type == 'testimonial']
This assumes your object is in an array (like your portable text) named content. You said your object was deeply nested, so that might require some finessing. I’d be interested in hearing if this gets the data you’re after.
May 1, 2021, 3:27 AM
I see, so would changing the type of testimonial to be a document fix that?
May 1, 2021, 4:31 AM
And the query doesn't seem to work, my full object looks like this
May 1, 2021, 4:32 AM
Well it seems like it works with documents, I guess that limitation of the querying should be in the docs
May 1, 2021, 4:41 AM
Yes, if it were a document you should be able to search for them with your original query.
The query I posted before would require you to use the GROQ API v2021-03-25 or later, which you can get access to in Vision by updating your sanity versions. I’m not sure
v1
has an equivalent query that drills down to the object.
May 1, 2021, 4:43 AM
Yes, if it were a document you should be able to search for them with your original query.
The query I posted before would require you to use the GROQ API v2021-03-25 or later, which you can get access to in Vision by updating your sanity versions. I’m not sure
v1
has an equivalent query that drills down to the object.
May 1, 2021, 4:43 AM
I see, thanks for your help!
May 3, 2021, 8:15 AM

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?