0

How to query an array in a firebase firestore doc, where the array must contain each item in the query.

I'm trying to create searchable categories on docs in a collection. I used an array to store the categories as strings. The issue is I can't figure out how to query the collection for docs that contain multiple categories like: [“category1”, “category2”], excluding any docs that don't contain both of these categories. So far from what I've seen in the documentation, I can only query for docs that have either of them. For example:

query(collectionRef, where('categories', 'in', ['category1', 'category2']));

This would return a doc that has either or both categories. I need a query that returns docs that have both categories. Something like:

query(collectionRef, where("categories", "array-contains", "category1", "and", "category2"))

Mr Ordinary
  • 4,886
  • 15
  • 57
  • 91
  • This type of query is currently not possible in Firestore, as there is no `array-contains-all` operator. See https://stackoverflow.com/a/46850943, https://stackoverflow.com/q/63852721, https://stackoverflow.com/a/59142459 – Frank van Puffelen Dec 09 '21 at 15:14

0 Answers0