Firebase 使用Flatter中的Firestore获取不同集合中的文档列表

Firebase 使用Flatter中的Firestore获取不同集合中的文档列表,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,我有一个拥有不同投资组合的用户列表。每个投资组合都有相同的属性,但也有一组投资。应该是这样的: Map user = { "name": "John", "portfolios": [ // collection { "title": "My portfolio 1", "investments": [ // collection {"title": "My investment 1.2"}, {"title": "My i

我有一个拥有不同投资组合的用户列表。每个投资组合都有相同的属性,但也有一组投资。应该是这样的:

Map user = {
  "name": "John",
  "portfolios": [ // collection
    {
      "title": "My portfolio 1",
      "investments": [ // collection
        {"title": "My investment 1.2"},
        {"title": "My investment 1.2"}
      ]
    },
    {
      "title": "My portfolio 2",
      "investments": [ // collection
        {"title": "My investment 2.1"},
        {"title": "My investment 2.2"},
        {"title": "My investment 2.3"}
      ]
    }
  ]
};
Future getMyPortfolios() async {
    final userId = await authService.getUserId();
    final portfolios = await Firestore.instance
        .collection('users')
        .document(userId)
        .collection('portfolios')
        .getDocuments();
    return portfolios.documents;
  }
为了得到我的投资组合,我做了如下工作:

Map user = {
  "name": "John",
  "portfolios": [ // collection
    {
      "title": "My portfolio 1",
      "investments": [ // collection
        {"title": "My investment 1.2"},
        {"title": "My investment 1.2"}
      ]
    },
    {
      "title": "My portfolio 2",
      "investments": [ // collection
        {"title": "My investment 2.1"},
        {"title": "My investment 2.2"},
        {"title": "My investment 2.3"}
      ]
    }
  ]
};
Future getMyPortfolios() async {
    final userId = await authService.getUserId();
    final portfolios = await Firestore.instance
        .collection('users')
        .document(userId)
        .collection('portfolios')
        .getDocuments();
    return portfolios.documents;
  }
但这只会返回带有标题的投资组合,而不会返回带有投资的内部集合


我可以用这些ID打另一个电话,但我想知道是否有更快的方法从特定用户那里获取所有信息。

没有更快的选项。您将需要一个单独的查询来获取任何其他集合中的文档,即使它们是子集合。Firestore查询总是“肤浅的”。在子集合中没有“嵌套文档”的“深层”查询。

我必须考虑我将使用免费帐户(至少现在)。如果我需要获得用户、投资组合和投资,这是3个独立的查询。我可以将这两个集合直接移动到用户内部的普通对象/数组中,并避免对每个用户进行这些额外调用。使用两个额外的查询不会导致显著的账单增加。您是按文档读取收费的,而不是按执行的查询收费的(除非该查询生成0个文档-在这种情况下,您只需按一次读取收费)。请参阅定价文档:我还要指出,存储可以无限增长的阵列将达到每个文档1MB的限制。此时,您的应用程序将被破坏,因为您将无法再添加数据。是否执行相同的查询并读取文档?当我写东西的时候,无论如何我都要做三次对不起,我不明白这个问题。