Your query currently looks for documents where the field owners
is a map with a nested array of string names
having rob
in it. But in your sample document the field with array of objects is members
. To use array-contains
with an array of objects, you need to pass in the complete object as is:
const owner = {id: '1', name: 'rob'}
const db = firebase.firestore()
const projectsCol = db.collection("projects")
const query = projectsCol.where("members", "array-contains", owner)
That being said, you need to know the id
field present in the object as well.
You could move members to a sub-collection instead of an array and use Collection Group queries. The database structure would look something like:
projects -> {projectId} -> members -> {memberId}
Each document in members subcollection would be similar to:
{
id: "1",
name: "rob",
...otherFields
}
Now you can run a collection group query on "members" which will return all documents where the name is "rob" and then you can use DocumentReference
to all matches documents to get the project ID.
const query = firebase.firestore().collectionGroup("members").where("name", "==", "rob")
query.get().then((snapshot) => {
console.log(snapshot.docs.map(d => d.ref.parent.parent.id))
})
This should log IDs of projects where the rob is a member.