Filtering results in Groq to only return "Lodge" model_type names in a park.

33 replies
Last updated: Jun 15, 2023
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:

*[_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
Is that first
park_unit
a 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
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
That returns empty
Jun 15, 2023, 1:44 PM
Just reading that link so might not reply instantly
Jun 15, 2023, 1:46 PM
No worries, I think I see where i messed up in it too.
Jun 15, 2023, 1:47 PM
Can you also share the schema for
park
?
Jun 15, 2023, 1:51 PM
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
So:
*[_type=='park' && name == $parkName]{
  _id, name, CaravanType[]->{name}
    }
Jun 15, 2023, 2:04 PM
And caravantype:
*[_type=='caravantype' && name == "Lodge"]{
  _id, name
}

https://gyazo.com/a1d9b8ba23148442395dae3cf5be61db
Jun 15, 2023, 2:06 PM
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
*[_type=='park' && name == $parkName]{
  _id, name, CaravanType[]->{
    name == "Lodge" => {
      name
    }
  }
}
Jun 15, 2023, 2:07 PM
Jun 15, 2023, 2:08 PM
Is there anyway to rid of the empty row?
Jun 15, 2023, 2:08 PM
the 0:
Jun 15, 2023, 2:08 PM
Might have to restructure it a bit to only project references if it finds the match.
Jun 15, 2023, 2:11 PM
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
Attribute or a string key expected
Jun 15, 2023, 2:13 PM
*[_type=='park' && name == $parkName]{
  _id, name, CaravanType[]{
    @->name == "Lodge" => {
      'name': @->name
    }
  }
}
Jun 15, 2023, 2:13 PM
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
Same as previous, still an empty row returned
Jun 15, 2023, 2:14 PM
It works, just with the extra row
Jun 15, 2023, 2:14 PM
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
caravanType is now empty haha
Jun 15, 2023, 2:24 PM
= null
Jun 15, 2023, 2:24 PM
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
Still null
Jun 15, 2023, 2:26 PM
The syntax highlighting looks wrong
Jun 15, 2023, 2:27 PM
Could it be anything to do with that?
Jun 15, 2023, 2:27 PM
🤔 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
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!
Jun 15, 2023, 5:02 PM
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
match
in 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.

Was this answer helpful?

Categorized in