Search Lessons, Code Snippets, and Videos
search by algolia
X
#native_cta# #native_desc# Sponsored by #native_company#

How to Query an Array in Firestore

Episode snippet written by Jeff Delaney
full courses and content on fireship.io

Health Check: This lesson was last reviewed on and tested with these packages:

  • Firebase v5.3

Find an issue? Let's fix it

How you ever wanted to make a query to Firestore for all documents with an array containing a certain value? This is now possible with a new query feature that was released in the JS SDK last week. In addition, the SDK also added support for the atomic addition and removal of elements on an array field on the 5.3.0 release.

This feature was introducted in Firebase SDK v5.3.0, so make sure you have the latest version installed.

Array Contains Query

Firebase introduced an array-contains operator that can be used with where to query array fields. It will return all documents that contain a the provided value in the array. Currently, this is only supported with one value, so don’t try chaining more than one of these in a single query.

const col = firestore.collection('carts');

const query = col.where('items', 'array-contains', 'fruit loops')

query.get(...)

Doesn’t get much easier than that.