Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Filter firestore collection by multiple ids

So in my database schema I have something like this

/collections/users/userId1 { 
   ...,
   id,
   relatedContracts: [id1, id2, id3]
}

/collections/contracts/contractId {
   ...,
   id,
}

In my firebase security rules I have this

    match /contracts/{contract} {
       function isOwnContract() {
           return request.auth.id == resource.data.creatorId || request.auth.id == recipientId
       }
       allow read, write: if isOwnContract()
     }

Now I am very confused on how to filter on the client to get only my related contracts. If the introduction of the security rules I can no longer do my filtering on the client.

I know this exists

firestore.collection(CONTRACTS).where(id, "==", contractId)

But how do I do this filtering based on multiple ids

like image 323
Robert Lemiesz Avatar asked Nov 07 '22 22:11

Robert Lemiesz


1 Answers

You currently can't fetch multiple ids per query on the client, so your choices are to either:

  1. Iterate each contract id in the user document, and fetch each document separately (which is not as bad as you probably think)
  2. Maintain another relationship in each contract that contains a list of users that have it as a relation. Then search the contracts collection for a user id in that list.
like image 161
Doug Stevenson Avatar answered Nov 14 '22 10:11

Doug Stevenson