0

I have an object that has an array called tags. It is array of objects. I need to query those objects that its tags contains a case insensitive string.

[
  {
    "_createdAt": "2022-02-18T09:16:27Z",
    "_id": "article-13000018493",
    "_rev": "LRHtyYM9ePAzIgMqDbhEWY",
    "_type": "article",
    "_updatedAt": "2022-02-23T14:29:00Z",
    "slug": {
      "current": "learn-to-kode"
    },
    "tags": [
      {
        "value": "Java"
      },
      {
        "value": "Python"
      },
      {
        "value": "JS and ts"
      },
      {
        "value": "React"
      }
    ],
    "tittel": "Learn to code"
  },
  {
    "_createdAt": "2022-02-18T09:16:27Z",
    "_id": "article-352398563",
    "_rev": "LRHtyYM9ePAzIgMqDbhEWY",
    "_type": "article",
    "_updatedAt": "2022-02-23T14:29:00Z",
    "slug": {
      "current": "learn-to-kode-js"
    },
    "tags": [
      {
        "value": "React"
      },
      {
        "value": "Next.js"
      },
      {
        "value": "js and TS"
      },
      {
        "value": "Vue"
      }
    ],
    "tittel": "Learn to code JS"
  }
]

I have used this query

*[_type == 'articles' &&  'js and TS' in tags[].value] {
  ...,
  tags[] { value }
}

It returns only the last object because the first object's tags contains JS and ts, not js and TS.

How to fetch both of the objects if the tags contains a case insensitive parameter?

This is link of my query on groq.dev.

https://groq.dev/P6RknxgQtDXJPG8UFJ6WyS

mahan
  • 12,366
  • 5
  • 48
  • 83
  • Did you end up finding the solution for this? I just hit it, and I'm stuck (I have all of a total of 1 day's worth of experience with GROQ, and this is tripping me up) My query basically reads. Select all Posts where that have a tag. I want to query by tag. (post can have one or many tags) I feel the is a slim air data model to the question you asked – Rohan Büchner Feb 01 '23 at 18:35

1 Answers1

0

I ended up using the lower() function to convert both value and the paramter ($tag) to lowercase letters.

*[_type == 'articles' &&  
  length(tags[lower(@.value) == lower($tag)]) > 0 &&
  !(_id in path("drafts.**"))
 ] {
      ...
 }
mahan
  • 12,366
  • 5
  • 48
  • 83