Filtering results in Groq to only return "Lodge" model_type names in a park.
33 replies
Last updated: Jun 15, 2023
S
How can I filter using groq to only return rersults that have "lodge" on the model_type name?
This is my json:
https://gyazo.com/404e0edaf09d19f7c90d61040a724dac
Here is what I have so far:
This is my json:
https://gyazo.com/404e0edaf09d19f7c90d61040a724dac
Here is what I have so far:
*[_type=='park' && name == $parkName && park_unit[]->parkasset[]->model_type.name == "Lodge"]{ _id, name, park_unit[]->{name,parkasset->{name, model_type->{name}}}}
Jun 15, 2023, 11:36 AM
N
Is that first
park_unita Lodge? If it is, it’s matching correctly, you’re just outputting all the units on the park that matches. I think you’ll want to query all the parks && match name, then you’ll want to do your conditional bit inside that query. Take a look at this. It’s the docs on conditionals https://www.sanity.io/docs/query-cheat-sheet#64a36d80be73
Jun 15, 2023, 1:37 PM
N
So maybe something like this?
*[_type=='park' && name == $parkName && park_unit[]->parkasset[]->model_type.name == "Lodge"]{ park_unit[]->parkasset[]->model_type.name == "Lodge" => { name, parkasset->{name, model_type->{name}} } }
Jun 15, 2023, 1:43 PM
S
That returns empty
Jun 15, 2023, 1:44 PM
S
Just reading that link so might not reply instantly
Jun 15, 2023, 1:46 PM
N
No worries, I think I see where i messed up in it too.
Jun 15, 2023, 1:47 PM
N
Can you also share the schema for
park?
Jun 15, 2023, 1:51 PM
S
Sorry, they've decided to change the names and stuff. Let me update you with the new ones and then I'll get you the schemas etc
Jun 15, 2023, 2:03 PM
S
So:
*[_type=='park' && name == $parkName]{ _id, name, CaravanType[]->{name} }
Jun 15, 2023, 2:04 PM
S
Jun 15, 2023, 2:04 PM
S
And caravantype:
https://gyazo.com/a1d9b8ba23148442395dae3cf5be61db
*[_type=='caravantype' && name == "Lodge"]{ _id, name }
https://gyazo.com/a1d9b8ba23148442395dae3cf5be61db
Jun 15, 2023, 2:06 PM
S
Here is the park schema:
export default { name: 'park', type: 'document', title: 'Park', fields: [ { name: 'name', type: 'string', title: 'Name' }, { name: 'CaravanType', title: 'Caravan Type', type: 'array', of: [ { type: 'reference', to: { type: 'caravantype' }, }, ], }, ] }
Jun 15, 2023, 2:06 PM
N
*[_type=='park' && name == $parkName]{ _id, name, CaravanType[]->{ name == "Lodge" => { name } } }
Jun 15, 2023, 2:07 PM
S
Okay nice, returns: https://gyazo.com/89e3cfee029cc4106fec64d9e9b14ce6
Jun 15, 2023, 2:08 PM
S
Is there anyway to rid of the empty row?
Jun 15, 2023, 2:08 PM
S
the 0:
Jun 15, 2023, 2:08 PM
N
Might have to restructure it a bit to only project references if it finds the match.
Jun 15, 2023, 2:11 PM
N
I think it’s like this, I might have to take a look at the docs if this isn’t quite right.
*[_type=='park' && name == $parkName]{ _id, name, CaravanType[]{ @->name == "Lodge" => { @->name } } }
Jun 15, 2023, 2:12 PM
S
Attribute or a string key expected
Jun 15, 2023, 2:13 PM
N
*[_type=='park' && name == $parkName]{ _id, name, CaravanType[]{ @->name == "Lodge" => { 'name': @->name } } }
Jun 15, 2023, 2:13 PM
N
If this gives an error it’s the first bit of line 3, and I’ll need to take a look at the docs
Jun 15, 2023, 2:14 PM
S
Same as previous, still an empty row returned
Jun 15, 2023, 2:14 PM
S
It works, just with the extra row
Jun 15, 2023, 2:14 PM
N
In the mighty words of Daft Punk,
One! More! Time!
*[_type=='park' && name == $parkName]{ _id, name, CaravanType[name match 'Lodge']->{ name } }
Jun 15, 2023, 2:23 PM
S
caravanType is now empty haha
Jun 15, 2023, 2:24 PM
S
= null
Jun 15, 2023, 2:24 PM
N
Does it need to expand the reference in the match maybe?
*[_type=='park' && name == $parkName]{ _id, name, CaravanType[@->name match 'Lodge']->{ name } }
Jun 15, 2023, 2:25 PM
S
Still null
Jun 15, 2023, 2:26 PM
S
Jun 15, 2023, 2:26 PM
S
The syntax highlighting looks wrong
Jun 15, 2023, 2:27 PM
S
Could it be anything to do with that?
Jun 15, 2023, 2:27 PM
N
🤔 I’m testing with an array of referenced companies and I’m able to match and return only the one specified by doing this.
Jun 15, 2023, 2:41 PM
S
I got it working using your old one:
*[_type=='park' && name == $parkName]{
"results" : park_unit[]->parkasset->model_type->{name == $type => {name}}
}
They changed the structure again which wasn't helping us
Thank you so much though!
*[_type=='park' && name == $parkName]{
"results" : park_unit[]->parkasset->model_type->{name == $type => {name}}
}
They changed the structure again which wasn't helping us
Thank you so much though!
Jun 15, 2023, 5:02 PM
N
Great to hear! Yeah, it can be a bit tricky to hit a moving target, but I’m glad it worked out. I got to learn more about
matchin the process. 👍
Jun 15, 2023, 6:36 PM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.