I have two collections: groups and users
I would like to perform a query that will return all groups where a user has a particular role assigned.
A group looks like this
{
"name": "myGroupName",
"members" : {
"firstUsersID" :
{
"roles" : ["user", "admin"]
},
"secondUsersID" :
{
"roles" : ["user" ]
},
}
}
Is there a way to perform queries in react native/web SDK which will return
Alternatively would I have to create an array/map on the user object listing their roles? Are there advantages/disadvantages to each route?
Currently I have the following which returns all groups
const colRef = collection(db, "groups");
const q = query(colRef)
const colSnap = await getDocs(q)
I figured it out I think
The following works
const colRef = collection(db, "groups");
const q = query(colRef, where("users." + user.uid + ".roles", "array-contains", "admin"))
In this case I am dynamically getting the uid of the current authenticated user but you could also hardcode it using
const q = query(colRef, where("users.firstUserID.roles", "array-contains", "admin"))