【问题标题】:Get detail information from master-detail collection in firestore从 Firestore 中的主从集合中获取详细信息
【发布时间】:2021-11-21 06:08:41
【问题描述】:

我使用 Flutter 和 firebase firestore 数据库创建了一个简单的应用程序。有“用户”集合,每个用户都有“帖子”集合。每个帖子可能有一个或多个由不同用户添加的帖子。 无论用户如何,我都在尝试获取所有帖子。但是,我当前的功能是为阅读记录而编写的,仅显示与登录用户相关的帖子,而不是所有帖子。

      Stream<QuerySnapshot> readItems({required String collectionName}) {
    CollectionReference detailCollection = _firestore
        .collection(mainCollectionName!)
        .doc(userUid)
        .collection(collectionName);

    return detailCollection.snapshots();
  }

在这里,我将'users'、'login user's uid'和'posts'分别作为mainCollectionName、userUid 和collectionName 传递。任何人都可以指导我如何获得所有帖子而不考虑用户?

【问题讨论】:

    标签: firebase flutter google-cloud-firestore collections


    【解决方案1】:

    搜索后我找到了解决方案here。以下方法给出了所需的输出。

      Stream<QuerySnapshot> readItems({required String collectionName}) {
        Query<Map<String, dynamic>> detailCollection = _firestore.collectionGroup(collectionName);
    
        return detailCollection.snapshots();
      }
    

    【讨论】:

      【解决方案2】:

      可以从集合中的所有文档中获取所有子集合数据。试试这个方法

      _firestore
        .collection(`${mainCollectionName}/*/${collectionName}`)
        .get()
      

      有关查询集合和子集合的更多详细信息,请参阅此页面 - https://firebase.googleblog.com/2019/06/understanding-collection-group-queries.html

      【讨论】:

      • 谢谢,@Raj Kumar,但我没有唤醒您的解决方案。
      猜你喜欢
      • 1970-01-01
      • 2022-08-19
      • 2010-12-06
      • 2010-11-02
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多