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

ⅰ亾dé卋堺 提交于 2020-01-12 05:40:06

问题


I have the following Firestore setup in use:

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.


回答1:


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

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
}



回答2:


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



来源:https://stackoverflow.com/questions/49543711/cloud-firestore-filter-based-on-object-content-via-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!