Register now - Learn how Tecovas roped in success with Sanity and Shopify 🤠

Help with GROQ query and filtering array in Slack thread

17 replies
Last updated: Mar 16, 2022
Hi everyone, I need some help with a GROQ query and I'm kinda stuck hereMy JSON looks like this

"policyIssues": [
    {
        "_key": "011a9d967951",
      "featureOnIssuePage": true,
      "policyIssue": {
        "_ref": "policy-types-climate-tech-commercialization",
        "_type": "reference"
      }
    },
    {
        "_key": "c112408ca014",
      "featureOnIssuePage": false,
      "policyIssue": {
        "_ref": "policy-types-climate-tech-rdd",
        "_type": "reference"
      }
    }
  ],
I want to retrieve documents where 'featureOnIssuePage' set to true and matches with the current reference. I was testing the graphql query but not sure how to get the array index dynamically

*["policy-types-climate-tech-commercialization" in policyIssues[].policyIssue._ref && true == policyIssues[0].featureOnIssuePage ]
[0..5]
{ _id, title}

Mar 16, 2022, 8:05 PM
Hey Devendra! Can you try filtering the array?
policyIssues[@.featureOnIssuePage]
Or, if it's part of your filter:

*["policy-types-climate-tech-commercialization" in policyIssues[].policyIssue._ref && length(policyIssues[@.featureOnIssuePage]) > 0]
[0..5]
{ _id, title}
Mar 16, 2022, 8:18 PM
It worked! Thank you so much!
Mar 16, 2022, 8:24 PM
You're welcome!!
Mar 16, 2022, 8:25 PM
It worked! Thank you so much!
Mar 16, 2022, 8:24 PM
You're welcome!!
Mar 16, 2022, 8:25 PM
It worked well when we have one item in array.However if we have more items, it will return that document as well if another item has 'featureOnIssuePage' == true
Mar 16, 2022, 8:38 PM
You need to apply that same filter in your projection.
Mar 16, 2022, 8:40 PM
I'm sorry! not sure if i get this one "You need to apply that same filter in your projection."
Mar 16, 2022, 8:46 PM
I'm sorry! not sure if i get this one "You need to apply that same filter in your projection."
Mar 16, 2022, 8:46 PM
No worries! In your current query, you're looking for documents with
policyIssues[@.featureOnIssuePage] > 0
, but then you're just pulling out the entirety of the array in your projection by asking for
{policyIssues}
. You need to filter that as well. For example:
*["policy-types-climate-tech-commercialization" in policyIssues[].policyIssue._ref && length(policyIssues[@.featureOnIssuePage]) > 0]
[0..5]
{ _id, title, policyIssues[@.featureOnIssuePage]}
Mar 16, 2022, 8:48 PM
Yeah! i'm wondering if there is a way to remove the item that doesn't match with the reference in graphql. I wanted to retrieve documents where 'featureOnIssuePage' set to true and matches with the current reference. ( AND comparison )
As JSON looks like this

 "policyIssues": [
    {
      "_key": "011a9d967951",
      "featureOnIssuePage": false,
      "policyIssue": {
        "_ref": "policy-types-climate-tech-commercialization",
        "_type": "reference"
      }
    },
    {
      "_key": "c112408ca014",
      "featureOnIssuePage": true,
      "policyIssue": {
        "_ref": "policy-types-climate-tech-rdd",
        "_type": "reference"
      }
    }
  ],
In this document we have 'featureOnIssuePage' == false and we have reference = 'policy-types-climate-tech-commercialization'
Mar 16, 2022, 8:57 PM
You have to add that to the projection, as well. It's the same reasoning as the other field.
*["policy-types-climate-tech-commercialization" in policyIssues[].policyIssue._ref && length(policyIssues[@.featureOnIssuePage]) > 0]
[0..5]
{ _id, title, policyIssues[@.featureOnIssuePage && @.policyIssue._ref == 'policy-types-climate-tech-commercialization']}
Mar 16, 2022, 9:44 PM
You have to add that to the projection, as well. It's the same reasoning as the other field.
*["policy-types-climate-tech-commercialization" in policyIssues[].policyIssue._ref && length(policyIssues[@.featureOnIssuePage]) > 0]
[0..5]
{ _id, title, policyIssues[@.featureOnIssuePage && @.policyIssue._ref == 'policy-types-climate-tech-commercialization']}
Mar 16, 2022, 9:44 PM
The first filter in your query is getting all of the documents that have those properties, then your projection is showing the fields you're asking for, which is why we also need to filter the
policyIssues
array.
Mar 16, 2022, 9:45 PM
Perfect thanks much!!
Mar 16, 2022, 9:51 PM
Perfect thanks much!!
Mar 16, 2022, 9:51 PM
Happy to help!
Mar 16, 2022, 9:53 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?