Firestore Tag Filtering With in Queries

Health Check

[email protected]7.3.0

With the release of the Firebase JS SDK version 7.3.0, we now have the ability to perform IN queries. It provides two new options to use with the where method, including in and array-contains-any.

A common use-case is tag or category filtering. For example, you may want to display a set of checkboxes, then run an inclusive query for any results that contain at least 1 checkbox.

Firestore IN Query

Option 1 - Document has ONE Tag

If the document can only have one tag or category at a time, you are likely looking for an in query. It takes an array of values (limited to a max of 10), then returns all matching documents. It is not perfectly analogous to SQL and you can also think of it as a logical OR query, i.e. all documents where color == red OR white OR blue

Data format for IN query

Data format for IN query

file_type_js_official in.js
const db = firebase.firestore();

const ref = db.collection('products');

const query = ref.where('color', 'in', ['red', 'white', 'blue']);

Option 2 - Document has MANY Tags

In other cases, your documents may be tagged with many values in an array. You fetch all documents that have a at least one value in the array with array-contains-any.

Data format for array-contains-any query

Data format for array-contains-any query

file_type_js_official firestore.js
const query = ref.where('colors', 'array-contains-any', ['red', 'white', 'blue']);

Simple, yet powerful!

Q&A Chat