Querying for car brands with specific colors in Sanity.io
16 replies
Last updated: Dec 6, 2023
C
Hey,
I've a GROQ question. Let's say I have following data and schemas:
• Brands (Car Brands like Audi, Volvo, VW, etc.)
• Cars (Cars like Audi A4, Audi A4, Volvo XC40, VW Golf, etc.)
• Colors (Black, White, Red, Green, etc.)
The Car schema has a reference to
one
Now I want to query for all car brands that have let's say
This GROQ query I made is partially working, but of course it only checks the first color (
Any way to check all referenced color slugs?
Current result, but of course there are many missing:
Thank you very much.
I've a GROQ question. Let's say I have following data and schemas:
• Brands (Car Brands like Audi, Volvo, VW, etc.)
• Cars (Cars like Audi A4, Audi A4, Volvo XC40, VW Golf, etc.)
• Colors (Black, White, Red, Green, etc.)
The Car schema has a reference to
one
brandand a reference to multiple
colors(as an array per schema definition), since every car is available in different and multiple colors.
// car.tsx const carSchema = defineType({ name: 'car', type: 'document', title: 'Car', //... fields: [ //.. defineField({ name: 'brand', title: 'Brand', type: 'reference', readOnly: true, to: [{ type: 'brand' }], }), //.. defineField({ name: 'colors', title: 'Colors', type: 'array', of: [ defineArrayMember({ type: 'reference', to: [{ type: 'colors' }], }), ], }), //.. ], preview: { //... }, }) export default carSchema
yellowcars available, my problem is, that I'm unsure how I can make a condition on a referenced slug.
This GROQ query I made is partially working, but of course it only checks the first color (
color[0]) for yellow, so I only get brands where the first set color of a car was yellow.
Any way to check all referenced color slugs?
*[_type=="brand" && count(*[_type == "car" && references(^._id) && color[0]->slug.current == "yellow"]) > 0]{ title, "slug": slug.current }
[ { "title": "Audi", "slug": "audi" } ]
Dec 6, 2023, 8:29 PM
L
user B
Sanity oficial Groq cheatset has an example on how to that, you could try something similar to:&& "yellow" in color[].slug.current]
Dec 6, 2023, 8:34 PM
L
See more at the oficial docs here: https://www.sanity.io/docs/query-cheat-sheet
Dec 6, 2023, 8:35 PM
C
wow now that was fast, thank you I'll take a look
Dec 6, 2023, 8:35 PM
C
user T
thank you, the thing is, with this query I get zero results, so []
*[_type=="brand" && count(*[_type == "car" && references(^._id) && "yellow" in colors[].slug.current]) > 0]{ title, "slug": slug.current }
*[_type=="brand" && count(*[_type == "car" && references(^._id) && colors[0]->slug.current == "yellow"]) > 0]{ title, "slug": slug.current }
*[_type=="brand" && count(*[_type == "car" && references(^._id) && colors[0]->slug.current == "yellow" || "yellow" in colors[].slug.current]) > 0]{ title, "slug": slug.current }
Dec 6, 2023, 8:44 PM
L
I think you can simplify your query. Brands are related to cars, so first get all the cards, then filter the cards by color, then return the distinct brands.
Dec 6, 2023, 8:53 PM
L
Color is a sub field of cars, not of an specific brand.
Dec 6, 2023, 8:56 PM
C
You are right I can sure flatten the query down and get all cars, and then their brands, but it's the same problem:
// work, it's basically the same as the querys above *[_type=="car" && colors[0]->slug.current == "yellow"] // does not work *[_type=="car" && "yellow" in colors[].slug.current] *[_type=="car" && "yellow" in colors[]->slug.current]
Dec 6, 2023, 8:58 PM
L
*[_type == "car" && count((colors[]->slug.current)[@ in ["yellow"]]) > 0)
Dec 6, 2023, 9:00 PM
L
*[_type=="car" && count((colors[]->slug.current)[@ in ["yellow"]]) > 0]
dinstinctor unique filter to make sure brands woulnd’t be repeated
Dec 6, 2023, 9:01 PM
L
In situations like this,
<http://sanity.io|sanity.io>cheatset is your best friend. https://www.sanity.io/docs/query-cheat-sheet
Dec 6, 2023, 9:02 PM
C
Lucas you got it right
Dec 6, 2023, 9:02 PM
L
Glad it worked 🙂
Dec 6, 2023, 9:02 PM
L
Have a look at the cheatset and you see special symbols in groq syntax like
@that are quite useful when querying references 🙂
Dec 6, 2023, 9:03 PM
C
Wow
user T
it really works this way, thank you vey much, I'll take a look to understand whyDec 6, 2023, 9:03 PM
C
It also works with the brands query now, combined with the cars, so the brands are unique there 🙂
Dec 6, 2023, 9:07 PM
L
Nice! Well done 🙂
Dec 6, 2023, 9:12 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.