๐Ÿ”ฎ Sanity Create is here. Writing is reinvented. Try now, no developer setup

Filtering child references in a Sanity query for a BMW make and model.

18 replies
Last updated: Jun 9, 2022
Is it possible to filter child references?
*[_type == "make" && name == 'BMW']{ 
          name, 
          "models": models[]->{ name, isHidden }
}
the above shows all
models
regardless if they are hidden or not. When I try to use:
*[_type == "make" && name == 'BMW']{ 
          name, 
          "models": models[isHidden != true]->{ name, isHidden }
}
It doesn't seem to work
๐Ÿคฏ
Jun 8, 2022, 10:55 PM
Hey
user J
! What version of the api are you using? Does it work if you try:
*[_type == "make" && name == 'BMW']{ 
          name, 
          "models": models[!@.isHidden]->{ name, isHidden }
}
Jun 8, 2022, 11:02 PM
2022-02-07
Jun 8, 2022, 11:03 PM
let me try
Jun 8, 2022, 11:03 PM
API should be ok, then!
Jun 8, 2022, 11:04 PM
hmm... not working ๐Ÿ˜ž
Jun 8, 2022, 11:09 PM
Hmm, I'm able to filter/expand children on my end, so it looks like we've got something wrong with our query. What does the data for one of these models look like?
Jun 8, 2022, 11:12 PM
I've also tried:
*[_type == "make" && name == 'BMW']{ 
          name, 
          "models": models[]->[isHidden == false]
}
but no luck with this one either
Jun 8, 2022, 11:14 PM
user M
, for now I've filtered out my results in javascript... But it would be useful to be able to filter in groq so it returns less data. Let me know if you get any other ideas. Thanks for your time.
Jun 8, 2022, 11:17 PM
If you share what your data for
models
looks like I can probably help.
Jun 9, 2022, 4:26 PM
Its pretty straight forward:
Make:

export default {
  name: 'make',
  title: 'Makes',
  type: 'document',
  fields: [
    {
      name: 'name',
      title: 'Name',
      type: 'string',
      validation: (Rule) => Rule.required(),
    },
    {
      name: 'models',
      title: 'Models',
      type: 'array',
      of: [
        {
          type: 'reference',
          to: [{ type: 'model' }],
        },
      ],
    },
    ...
Model:

export default {
  name: 'model',
  title: 'Models',
  type: 'document',
  fields: [
    {
      name: 'name',
      title: 'Model Name',
      type: 'string',
      validation: (Rule) => Rule.required(),
    },
    {
      name: 'isHidden',
      title: 'Hidden?',
      type: 'boolean',
      initialValue: false,
    },
Jun 9, 2022, 4:31 PM
Have you checked that
isHidden
is actually set on the documents you're querying?
Jun 9, 2022, 4:33 PM
yes ofcourse:
Jun 9, 2022, 4:36 PM
๐Ÿคท hmmmm. Is your dataset public? If so, can you share the url that vision outputs for that query (it's right above the pane that contains your results).
Jun 9, 2022, 4:38 PM
Oh wait, maybe:
*[_type == "make" && name == 'BMW']{ 
          name, 
          "models": models[!@->.isHidden]->{ name, isHidden }
}
Jun 9, 2022, 4:39 PM
YAYAYAYAYAYAY!!!! WORKS :))))... definitely this example needs to be added to https://www.sanity.io/docs/query-cheat-sheet#3949cadc7524
Jun 9, 2022, 4:41 PM
Yes! So glad we got it figured out!
Jun 9, 2022, 4:43 PM
thanks so much for your help ๐Ÿ™‚
Jun 9, 2022, 4:43 PM
Very happy to help!
Jun 9, 2022, 4:45 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?