如何在Cloud Firestore(web)中获取所有没有名称的集合和文档

时间:2017-11-15 12:49:46

标签: javascript firebase google-cloud-firestore

例如,我有这种结构的数据库:

  • 太阳能系统(collecton)
    • 地球(文件)
      • 欧亚大陆(收藏)
        • 法国(文件)
          • 人口:65 000 000
          • 面积:640 000
        • 德国(文件)
        • 俄罗斯(文件)
      • 南美(收藏)
      • 非洲(收藏)
    • 维纳斯(文件)
    • 火星(文件)

如果我使用此代码:

db.collection("Solar system").doc("Earth").collection("Eurasia").get().then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                console.log(doc.id, " => ", doc);
            });
        });

我将在控制台中获得一个国家/地区列表:

  • 法国
  • 德国
  • 俄罗斯

但是如果我不知道我的数据库中的所有职位并且我只想获得所有职位?

例如,我不知道我的收藏品“地球”中有多少大洲,我想在控制台中打印所有大洲的所有国家的列表。

换句话说,我怎样才能在查询中指定集合和文档的名称?

db.collection("Solar system").doc(?????).collection(?????).get().then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                console.log(doc.id, " => ", doc);
            });
        });

1 个答案:

答案 0 :(得分:0)

您可以使用根级别集合:

 - Planets
     - Earth { id: earth }
     - Venus { id: venus }
     - Mars  { id: mars }
 - Continents
     - Europe { id: europe, planet: earth } 
     - Asia   { id: asia, planet: earth }
 - Countries
     - Germany { id: germany, continent: europe, planet: earth }
     - France { id: france, continent: europe, planet: earth }

根据你的问题“例如,我不知道我的收藏中有多少大陆”地球“,我想在控制台中打印所有大洲的所有国家的列表。”,通过这种结构你可以这样做方式:

db.collection("Countries").where("planet", "==","earth")

所有“欧洲”大陆的国家:

db.collection("Countries").where("continent", "==", "europe")