0

I've been stuck for a while trying to optimise my groq query.

I have page content that contains an array objects (different languages).

I've been playing around in Sanity Vision to see how I can filter the output so that I only get the content in the correct language.

//query
*[_type == "home"]{
  content[]{
    "language": metaData.language ->.language,
  },
}

// query result
"result":[
  0:{
    "content":[
      0:{
        "language":"en-AU"
      }
      1:{
        "language":"th-TH"
      }
    ]
  }
]

I wanted to get just the 1 content that matches the language. I tried this but it didn't work

*[_type == "home"]{
  content[]{
    ...,
    "language": metaData.language ->.language,
  },
}[0][content[].language == "en-AU"]

Does anyone know how?

Thank you!

Tanpo
  • 233
  • 3
  • 18

1 Answers1

1

Finally found an answer

I realise where there is an array inside the return data, you can filter it further using another [], in this case [metaData.language->.language match $language]


  *[_type == "home"]{
    content[metaData.language->.language == $language]{
      ...,
      metaData {
        ...,
        language->
      }
    }[0]
  }[0]
Tanpo
  • 233
  • 3
  • 18
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 25 '21 at 00:04
  • Just keep in mind that `match` is for text, and obeys text-oriented normalization/tokenization rules. It probably doesn't work the way you expect. You should use `==` here. – Alexander Staubo Dec 26 '21 at 00:18