如何汇总firebase数据库中的值#AskFirebase [英] how to summarize values in firebase database #AskFirebase

查看:141
本文介绍了如何汇总firebase数据库中的值#AskFirebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在firebase数据库中总结数值。

注意:这是一个简化的非真实项目,以便于解释。所以,如果JSON数据有错误,请道歉。这是为了演示的目的,我会适应我的实际需求。
完整的项目描述与真实数据在这里:最佳实践总结FirebaseDatabase值 ,但似乎太具体。



假设我收集多辆车的重量数据,每一次他们在规模。



因此,一些示例数据可能如下所示:

  weightTable {
weightKey1 {
carKey :carOne,
carWeight:1.4
},
weightKey2 {
carKey:carOne,
carWeight:1.9

weightKey3 {
carKey:carTwo,
carWeight:1.1
},
weightKey4 {
carKey: carThree,
carWeight:1.7
}
}



<现在我想要一个单独的表,我只有所有的重量数据的总结,carKey分组。所以我得到

p>

  summTable {
carOne{
weightSum:3.3,
cnt:2
},
carTwo{
weightSum:1.1,
cnt:1
},
carThree{
weightSum:1.7,
cnt:1
}
}

weightSum是carWeight的摘要。

cnt是总结的数据计数。

最后我想要一个表格,列出所有汽车的汇总。
$ b

  totalCarWeight {
weightSum:6,1,
cnt:4
}

sumTable用于在Android应用程序和网页中填充视图以显示摘要。如果他们在摘要中点击一辆汽车,他们会得到这辆车的详细视图,并收集每个重量。
所以汇总数据应该是基于尽力而为的最新的。

可能的问题我看到:


  • 设备没有网络连接,并将数据添加到weightTable;

  • 多个设备将数据添加到weightTable。




任何提示怎么做?

在一个正常的SQL数据库中,我们可以要求数据库在查询中进行汇总或计数。



希望这个更容易理解和回答。 / p>

在Android或JavaScript中的一些指导将会很棒。

您可以通过weightTable来查询Firebase,其中carKey等于carOne,并在您的应用程序中对值进行求和。 ://github.com/angular/angularfire2/blob/master/docs/4-querying-lists.mdrel =nofollow noreferrer> docs



<对于Firebase SDK(web),请参阅排序和过滤部分的:文档



如果您仍然需要其他表格(summTables,totalCarWeight),则每次写入weightTable时都需要更新它们 - 所以您可以在应用程序中使用它(或者更好的方法)适用于Firebase的Google云端函数。因此,每次写入weightTable时,都会触发更新其他表的功能。



对于Firebase的Google Cloud Functions,请参阅: Jen Person视频及其他视频 Firebase YT频道


I have to summarize values in firebase database.

Note: this is a simplified non real project to make it easier to explain. So apologize if the JSON data has an error. It is for demonstration purposes and I will adjust to my real needs. Full project description with real data is here: best practize summarize FirebaseDatabase values, but seems too specific.

Assuming I collect weight data for multiple cars, every time they are on the scale.

So some sample data could look like:

weightTable{
  weightKey1{
    "carKey" : "carOne",
    "carWeight" : 1.4
  },
  weightKey2{
    "carKey" : "carOne",
    "carWeight" : 1.9
  },
  weightKey3{
    "carKey" : "carTwo",
    "carWeight" : 1.1
  },
  weightKey4{
    "carKey" : "carThree",
    "carWeight" : 1.7
  }
}

I now want to have a separate "table" where I only have the summary of all the weight data, grouped by "carKey".

So I get

summTable{
  "carOne"{
    "weightSum" : 3.3,
    "cnt" : 2
  },
  "carTwo"{
    "weightSum" : 1.1,
    "cnt" : 1
  },
  "carThree"{
    "weightSum" : 1.7,
    "cnt" : 1
  }
}

"weightSum" is the summary of "carWeight".

"cnt" is the count of data summarized.

Finally I want to have a table, that list the summary of all cars

totalCarWeight{
        "weightSum" : 6,1,
        "cnt" : 4
}

sumTable is used to populate view in an Android app and webpage to display the summary. If they click on a car in the summary, they get the detailed view for this car, with each weight collected. So summary data should be up to date on best effort base.

Possible Issues I see:

  • Device has no network connectivity and adds data to the "weightTable";

  • Multiple device add data to the "weightTable".

Any hints how to do this ?

On a normal SQL database we can ask the database to make a summary or count entries in a query.

Hope this is easier to understand and answer now.

Some guidance in either Android or javascript would be great.

解决方案

You can query Firebase over the "weightTable" where "carKey" equalTo "carOne" and sum values in your app.

For AngularFire querying see: docs

For Firebase SDK (web) see sorting and filtering section of: docs

If you still need other tables ("summTables", "totalCarWeight") you will need to update them everytime you write to "weightTable" - so you can do it in your app or (maybe better way) use Google Cloud Functions for Firebase. So everytime you write to "weightTable" it will fire function that updates other tables.

For Google Cloud Functions for Firebase see: Jen Person video and her other videos on Firebase YT channel.

这篇关于如何汇总firebase数据库中的值#AskFirebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆