9
votes

I have the following Firestore setup in use:

enter image description here

Now I'd like to receive all documents where the current user (uid given) is in the users object/list. users is an object of references to thje users collection.

Ideally I would want to use this filter from Flutter with the cloud_firestore package, however for now I'm just interested if this is possible at all.

2

2 Answers

12
votes

I found this post which explains, that is not currently possible how I imagined it. I altered my setup to this:

enter image description here

I can now use this query from Flutter to receive the chats for a given user

Firestore.instance
    .collection('chats')
    .where('users.' + _auth.currentUser.uid, isEqualTo: true)
    .snapshots
    .listen((data) {...});

I'm also using this rule to make sure that the user can only access the chats in which he is participating:

match /chats/{chatId} {
  allow read: if resource.users[request.auth.uid];
  // write rule yet to create
}
1
votes

Yes. This is possible. However you will need to store you user references as a map of values and create a reference to query with in your client (based on the user ID). Take a look at the documentation: Working with Arrays, Lists and Sets