Cloud Firestore:基于对象内容的过滤(通过 Flutter)

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

我正在使用以下 Firestore 设置:

现在我想接收当前用户(给定的 uid)在用户 object/list 中的所有文档。 users 是对 thje users 集合的引用对象。

理想情况下,我想将 Flutter 中的过滤器与 cloud_firestore 包一起使用,但是现在我只是想知道这是否可行。

是的。这个有可能。但是,您需要将用户引用存储为值映射,并创建一个引用以在您的客户端中进行查询(基于用户 ID)。查看文档:Working with Arrays, Lists and Sets

我发现 post 这说明,目前我想象的不可能。 我将设置更改为:

我现在可以使用来自 Flutter 的查询来接收给定用户的聊天记录

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

我还使用此规则来确保用户只能访问他参与的聊天:

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