Firestore: Query by item in array of document

Jaap Weijland picture Jaap Weijland · Oct 20, 2017 · Viewed 36.1k times · Source

I have a document "user" which has a key called "photos". Here is an array of strings being saved, namely the IDs of the "photo" documents.

I want to query "user" by this "photos" field: I want all users who have this ID in the "photos" property.

Is this possible through firestore?

Answer

Dan McGrath picture Dan McGrath · Oct 20, 2017

See Henry's answer, as we've no made Array Contains queries available.

Unfortunately not yet, although it's on our roadmap.

In the meantime, you'll need to use a map instead, in the form of:

photos: {
    id1: true
    id2: true
}

Now you can find all users with id1 by filtering by photos.id1 == true.

Read more about querying such sets in the Firebase documentation.