【问题标题】:How to get all collections and documents without their names in Cloud Firestore (web)如何在 Cloud Firestore(网络)中获取所有没有名称的集合和文档
【发布时间】:2017-11-15 12:49:46
【问题描述】:

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

  • 太阳系 (colleton)
    • 地球(文档)
      • 欧亚大陆(收藏)
        • 法国(文件)
          • 人口: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);
            });
        });

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    您可以使用根级集合:

     - 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")
    

    【讨论】:

      猜你喜欢
      • 2019-08-05
      • 2019-12-29
      • 2021-05-09
      • 2021-03-27
      • 2020-04-16
      • 1970-01-01
      • 2018-04-03
      相关资源
      最近更新 更多