【问题标题】:How to Use Groupby in Firebase Database如何在 Firebase 数据库中使用 Groupby
【发布时间】:2017-04-19 13:46:07
【问题描述】:

我的 Firebase 数据库有以下数据:

   datas=[
    { "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
    { "category" : "Content Server", "hits" : 1, "bytes" : 47412 }, 
    { "category" : "Search Engines", "hits" : 1, "bytes" : 7601 }, 
    { "category" : "Content Server", "hits" : 1, "bytes" : 24210 }, 
    { "category" : "Internet Services", "hits" : 1, "bytes" : 3690 }, 
    { "category" : "Search Engines", "hits" : 6, "bytes" : 613036 }, 
    { "category" : "Search Engines", "hits" : 1, "bytes" : 2858 } 
    ];

我们如何在 Firebase 数据库中制作与此等效的内容 SQL查询?:

SELECT category, sum(hits), sum(bytes) 
FROM datas 
GROUP BY category

【问题讨论】:

    标签: javascript firebase firebase-realtime-database nosql


    【解决方案1】:

    Firebase 是一个 NoSQL 数据库。尝试将 SQL 范式 1:1 映射到 NoSQL 数据库肯定会让您感到痛苦。

    大多数 NoSQL 数据库的查询能力有限,Firebase 也不例外。这意味着您通常必须以允许应用程序需要的查询的方式对数据进行建模。

    对于这个特定的用例,我建议修改数据结构以反映您尝试访问的实际层次结构和聚合数据:

    categoryAggregates:
      "Content Server": { hitsCount: 3, bytesCount: 88930 }
      "Internet Services": { hitsCount: 1, bytesCount: 3690 }
      "Search Engines": { hitsCount: 3, bytesCount: 1233673 }
    

    添加这样的结构最终会复制数据并使其更新更加复杂/昂贵。但是您可能可以想象,使用这种结构访问聚合变得非常便宜。这也是 NoSQL 数据库的常见模式,它们针对读取性能进行了优化,但代价是更复杂/更昂贵的写入。

    我建议先阅读NoSQL data modeling,然后观看我们的视频系列Firebase for SQL developers,然后阅读一些questions that have already tried this

    【讨论】:

    • 谢谢。我会朝着你建议的方向前进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2017-09-29
    • 2019-08-07
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多