✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Understanding nested object queries in Groq for matching text and retrieving slugs.

6 replies
Last updated: Jul 22, 2022
Hey! i'm new to groq and I'm struggling with understanding how I can make a query on a nested object.Here's what I'm trying to do: I'm trying to get a list of all the slugs where the text (lorem example below) matches a certain word. Any help would be appreciated, I'm a bit inexperienced with groq
🙂

{

"_createdAt": "2022-07-01T06:15:15Z",

"_id": "ipsum",

"_rev": "lorem",

"_type": "post",

"_updatedAt": "2022-07-07T04:00:02Z",

"contentBlocks": [

{

"_key": "somenu12312",

"_type": "textContent",

"mainText": [

{

"_key": "somenum123123",

"_type": "block",

"children": [

{

"_key": "othernum123213",

"_type": "span",

"marks": [],

"text": "omnis-aut-voluptatem Culpa beatae omnis corporis in enim. Sit quo mollitia et omnis ea sit praesentium rerum."

}

],

"markDefs": [],

"style": "normal"

}

]

}

],

"slug": {

"_type": "slug",

"current": "ipsum"

},

"title": "lorem"

}
Jul 21, 2022, 12:16 PM
Closest I've come by my own understanding is:
*[_type=="post"] {

"slugs": *[contentBlock[].mainText[].children[].text match 'corporis']{slug}

}
Jul 21, 2022, 1:04 PM
Hey
user Q
! You're on the right track. You will need to drill down into arrays using the
[]
operator. Does the query you put together work for you or do you still need help putting it together?
Jul 21, 2022, 5:14 PM
Hey
user M
no it doesnt work im afraid. Does any immediate errors jump out to you?
Thanks!
Jul 21, 2022, 5:22 PM
No! But I can dig deeper! Can you share your schema so I can replicate it?
Jul 21, 2022, 5:24 PM
Ill get to it first hing in the morning 😊
Jul 21, 2022, 6:00 PM
I solved it.
`groq`{*[_type=="post" && contentBlocks[].mainText[].children[].text match ${author.fullname} || lead match ${author.fullname}]{`

"slug":slug.current

}
`}``

I do get a string literal error if I try to do:

{

slugs: *[_type=="post" && contentBlocks[].mainText[].children[].text match "horst" || lead match "horst"]{

"slug":slug.current

}

}
Which doesn't make much sense to me
Jul 22, 2022, 8:41 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?