Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Querying nested arrays in Sanity schema

8 replies
Last updated: Aug 28, 2020
For a document with schema (I removed unrelated parts):
{
  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",
                  },
                ],
              },
            },
          ],
        },
      ],
    },
  ],
},
I want to fetch based on
provider[].carrierId
&&
activation[]._type == "busmaticScheduler" && "upis-city" in activation[].plugins
, but that results in an empty result, when queried as such:
*[
  _type == "client"
  && provider[].carrierId == 5
  && activation[]._type == "busmaticScheduler"
  && "upis-city" in activation[].plugins
]
Or:

*[
  _type == "client"
  && provider[].carrierId == 5
  && activation[]._type == "busmaticScheduler"
  && activation["upis-city" in plugins]
]
Maybe I am trying to accomplish the impossible? Or have I got something wrong?
Aug 28, 2020, 8:21 AM
These will both return arrays, so comparing them to a string doesn't make sense?
provider[].carrierId
activation[]._type
Try

5 in activation[]._type &&
"busmaticScheduler" in activation[]._type
Aug 28, 2020, 10:26 AM
?
Aug 28, 2020, 10:26 AM
Well, but the query works except for the
"upis-city" in activation[].plugins
Aug 28, 2020, 10:37 AM
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
But you're right on that, I can rework them all to `in`'s. The real problem though is with the
in activation[].plugins
part.
Aug 28, 2020, 10:41 AM
*[
  _type == "client"
  && "upis" in provider[]._type
  && 5 in provider[].carrierId
  && "busmaticScheduler" in activation[]._type
  && defined(activation[_type == "busmaticScheduler" && "upis-city" in plugins])
] {
  name,
  sanity
}
This seems to have done it.
Aug 28, 2020, 10:46 AM
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
Had to use the
defined
function on
plugins
part, 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.

Was this answer helpful?