Filter by tag array


#1

How can I filter by an array? For example, I have a field for category and I can do:

const body = {
  filter: {
    published: true,
    'category.display': 'MyCategory',
  },
  limit: 10,
  sort: { _created: -1 },
  simple: 1
};

But, I can’t do this for an array. The only way I have found is to write the index directly, but I want it to search all the elements of the array.

const body = {
  filter: {
    published: true,
    'tags.0.display': 'MyTag',
  },
  limit: 10,
  sort: { _created: -1 },
  simple: 1
};

Is there any way to filter the entries looking in an array?


#2

An old post here suggests this should be possible using:

filter: {
  tags: {
    $all: ["tag1", "tag2"]
  }
}

However, I just tested it and it doesn’t work :confused:

Maybe Artur will see this and help.


#3

you need to use something like below:

{
  "filter": {
    "tags": {
      "$in": ["tag1", "tag2"]
    }
  }
}