Querying nested arrays in Sanity schema
8 replies
Last updated: Aug 28, 2020
C
For a document with schema (I removed unrelated parts):
I want to fetch based on
, but that results in an empty result, when queried as such:
Or:
Maybe I am trying to accomplish the impossible? Or have I got something wrong?
{ type: "document", name: "client", fields: [ /*...*/, { type: "array", name: "provider", of: [ { type: "object", name: "upis", fields: [ /*...*/, { type: "number", name: "carrierId", title: "Carrier ID", }, ], }, ], }, { type: "array", name: "activation", of: [ { type: "object", name: "upis", title: "Ūpis (Cashreg)", fields: [ /*...*/ ], }, { type: "object", name: "busmaticScheduler", title: "Busmatic Scheduler", fields: [ { type: "array", name: "plugins", title: "Plugins", of: [{ type: "string" }], options: { list: [ { title: "Ūpis for City", value: "upis-city", }, { title: "Import API", value: "import-api", }, ], }, }, ], }, ], }, ], },
provider[].carrierId&&
activation[]._type == "busmaticScheduler" && "upis-city" in activation[].plugins
*[ _type == "client" && provider[].carrierId == 5 && activation[]._type == "busmaticScheduler" && "upis-city" in activation[].plugins ]
*[ _type == "client" && provider[].carrierId == 5 && activation[]._type == "busmaticScheduler" && activation["upis-city" in plugins] ]
Aug 28, 2020, 8:21 AM
J
These will both return arrays, so comparing them to a string doesn't make sense?
Try
provider[].carrierId activation[]._type
5 in activation[]._type && "busmaticScheduler" in activation[]._type
Aug 28, 2020, 10:26 AM
J
?
Aug 28, 2020, 10:26 AM
C
Well, but the query works except for the
"upis-city" in activation[].plugins
Aug 28, 2020, 10:37 AM
C
And not only it works, but also returns the expected results. (Or maybe I am hitting an unexpected behavior and my data appears to be OK with it for now)
Aug 28, 2020, 10:39 AM
C
But you're right on that, I can rework them all to `in`'s. The real problem though is with the
in activation[].pluginspart.
Aug 28, 2020, 10:41 AM
C
*[ _type == "client" && "upis" in provider[]._type && 5 in provider[].carrierId && "busmaticScheduler" in activation[]._type && defined(activation[_type == "busmaticScheduler" && "upis-city" in plugins]) ] { name, sanity }
Aug 28, 2020, 10:46 AM
C
And actually, I can reduce it to:
*[ _type == "client" && "upis" in provider[]._type && $carrierId in provider[].carrierId && defined(activation[_type == "busmaticScheduler" && "upis-city" in plugins]) ] { name, sanity }
Aug 28, 2020, 10:49 AM
C
Had to use the
definedfunction on
pluginspart, otherwise the query didn't return anything.
Aug 28, 2020, 10:50 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.