👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Querying nested attributes in Sanity.io model and upgrading to the latest API version.

15 replies
Last updated: May 31, 2021
Hi! I have the following model:• A
company
with an array of
team
objects (no references, only nested objects) called
companyTeams[]
• Each
company
and
team
has their own unique
slug
I'm trying to write a query where I filter by both
company.slug.current
and
team.slug.current
, but I'm not able to get the
$teamSlug in companyTeams[].slug[].current[]
filter to work. I've tried all combinations (
slug[].current
,
slug.current[]
,
slug[].current[]
), and if I create a new string attribute for the
team
objects called
slugString
, I get the following filter to work as intended:
$teamSlug in companyTeams[].slugString[]

Is it not possible to filter by a sub-sub-attribute, i.e.
slug.current
, with the
in
operator?
See the attached screenshot for my current workaround using `slugString`:
May 31, 2021, 9:35 AM
slug
is an object like
{"current": "foo"}
? If so, you should be able to use:
$teamSlug in companyTeams[].slug.current
May 31, 2021, 9:42 AM
We realize that the semantics of the
[]
operator can be confusing! Think of
[]
as being “map over”. So
a[].b
is equivalent to JavaScript
a.map(val => val.b)
.
May 31, 2021, 9:43 AM
The
[]
operator becomes a flattening operator if added to the end of an array, which means that something like
parents[].children
returns an array of arrays (i.e. each parent’s children as a nested array), but
parents[].children[]
returns a flat array of all the children.
May 31, 2021, 9:44 AM
Thanks for the clarification! However,
$teamSlug in companyTeams[].slug.current
still returns
{}
May 31, 2021, 9:46 AM
I'm using the v1 API version btw
May 31, 2021, 9:47 AM
You should be using
v2021-03-25
. The array handling is very buggy in v1.
May 31, 2021, 9:48 AM
The new version also has much better performance.
May 31, 2021, 9:48 AM
Ahh, I wasn't aware of that. Will try the new version now. Any breaking changes from v1 to
v2021-03-25
?
May 31, 2021, 9:52 AM
May 31, 2021, 9:59 AM
Scroll down to “Breaking changes from v1 to v2021-03-25”
May 31, 2021, 9:59 AM
The breaking changes are mostly fixes 🙂
May 31, 2021, 10:00 AM
Perfect, thanks for the help!
May 31, 2021, 10:16 AM
👍
May 31, 2021, 10:27 AM
Everything worked as expected now – and super-easy upgrade –
null
vs.
{}
return value was the only breaking change for us
May 31, 2021, 10:31 AM
Great!
May 31, 2021, 10:35 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?