Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore: Filter based on object content (via Flutter)

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.

like image 773
tobire Avatar asked Mar 28 '18 20:03

tobire


2 Answers

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
}
like image 129
tobire Avatar answered Oct 31 '22 18:10

tobire


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

like image 40
Jason Berryman Avatar answered Oct 31 '22 20:10

Jason Berryman