0

I have a firestore collection with many document and all these documents include an array field (called "notFor") with a list of uid. In some cases the array contains only 1 value in some other cases it contains more than 1 value. Example

docA

  • notFor: "userB", "userC"

docB

  • notFor: "userB"

docC

  • notFor: "userA", "userC"

Is there any suggestion on how I could find a document in that collection that does not include "userB"? I basically would like to find docB by excluding all the docs where userC is present

I am writing in flutter however this is not so relevant for this case, I am just interested to understand if I can do something like this with the firestore operators.

thanks!

M4trix Dev
  • 1,828
  • 3
  • 23
  • 48
  • I confirm this is a hard-constraint from `Firebase`: You cannot query arrays in `Firestore` based on `values` **they don't include**. It is not possible in Firestore. Now, some potential workaround that may or may not work for your specific case: **Option1:** Change your field `notFor` into a new field `for`. This way you can query using arrayContains. Now, you will have the list of authorized users only (This worked well for my project) **Option2:** Query all values then perform your filter locally on client-side. I understand this is not suitable in most use cases. – Canada2000 Oct 22 '21 at 19:36
  • Hey may thanks. I was already looking into opinion 1. My only problem is that the list of user in the notFor group is small while the ones in the β€œfor” is very large (thousands) so I would write thousands of records. Option 2 is not good because it would increase my cost with firebase since I would need to download all the data and then filter it – M4trix Dev Oct 23 '21 at 21:30

0 Answers0