Firestore to query by an array's field value

sisi mendel picture sisi mendel · Jan 7, 2019 · Viewed 7.6k times · Source

I'm trying to run a simple query, where I search for a document that contains a value inside an object array.

For instance, look at my database structure:

firestore db

I want to run a query similar to this:

db.collection('identites').where("partyMembers", "array-contains", {name: "John Travolta"})

What is the correct way to achieve this, is it even possible with Firestore?

Thanks.

Answer

Renaud Tarnec picture Renaud Tarnec · Jan 7, 2019

As Frank has explained in his answer it is not possible, with array-contains, to query for a specific property of an object stored in an array.

However, there is a possible workaround: it is actually possible to query for the entire object, as follows, in your case:

db.collection('identites').where("partyMembers", "array-contains", {id: "7LNK....", name: "John Travolta"})

Maybe this approach will suit your needs (or maybe not....).