是否可以在Firestore中运行聚合查询? [英] Is it possible to run aggregation queries in Firestore?

查看:54
本文介绍了是否可以在Firestore中运行聚合查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

堆栈:

Ionic
Nodejs/Express
Cloud Firestore

我的任务是编写一个应用程序,该应用程序以"day"格式记录日期,并具有该天的余额,并使用Chart.js在图表中显示该数据.有间隔按钮可让您在天",周"和月"之间进行切换,以将日期分为相应的间隔.

I am tasked with writing an app that takes dates in "day" format, with a balance for that day, and displaying that data in a chart using Chart.js. There are interval buttons that allow you to change between "day", "week", and "month" that is supposed to group the dates into respective intervals.

当前使用1个收藏集可以正常工作. 几天"和几周"都可以工作,但是一旦我们进入拥有大量数据的月",Firestore就会在我的后端自杀.它尝试轮询的数据量太大.我目前在后端使用天"对周"和月"进行汇总.

This currently works fine using 1 collection. "days" and "weeks" both work, but once we get to "month" with large amounts of data Firestore kills itself in my backend. The amount of data it tries to poll is too large. I currently run aggregation for "weeks" and "months" in the backend using the "days".

我在文档中只能找到的汇总文档是: https://firebase .google.com/docs/firestore/solutions/aggregation 不会为我提供结果,而是将其存储在对我没有帮助的集合中.该应用程序可以在单个日期更改余额,这会在事实发生后在余额中产生连锁反应-因此,我必须在间隔更改时生成值.

The only aggregation documentation I could find in the docs was: https://firebase.google.com/docs/firestore/solutions/aggregation which doesn't give me a result, it stores it in a collection which doesn't help me. The app can change the balance on a single date which causes a ripple effect in the balances after the fact - so I have to generate the values on interval change.

是否存在类似的东西,还是我坚持创建3个独立的集合(天/周/月)并轮询所需的集合?

Does something like this exist or am I stuck with creating 3 separate collections, days/weeks/months and polling the desired collection?

推荐答案

来自文档您链接:

Cloud Firestore不支持本机聚合查询.

Cloud Firestore does not support native aggregation queries.

因此,您可以很好地回答标题中的问题:Firestore不具有内置功能来在数据库服务器上运行聚合.

So that pretty much answers the question in your title: Firestore does not have a built-in capability to run aggregations on the database server.

常见的解决方案是:

  1. 在客户端上运行聚合

  1. Run the aggregations on the client

这听起来像是您现在正在做的事情:您正在下载内部的所有数据,然后将其聚合到客户端上.这种方法适用于小型数据集,但如果仅在客户端中显示聚合,则可能会下载比所需更多的数据.这就是为什么如果您的数据集可能很大(在使用Firestore时通常会出现)的情况下,您应该考虑使用替代方案.

It sounds like this is what you're doing now: you're downloading all data for the internal, and then aggregate it on the client. This approach can work well for small data sets, but if you're only displaying the aggregates in the client, you are likely downloading much more data than needed. That's why you should consider alternatives if you data set may be large, which it typically will be(come) when you use Firestore.

每次数据更改时更新汇总

Update aggregates every time the data changes

在这种情况下,您将汇总值存储在数据库中,并在每次写入汇总值时对其进行更新.该文档显示了以此方式计算移动平均值的示例,该示例完全不需要查询,因此可以缩放到任何大小的数据集.

In this scenario you store the aggregated value in the database and update it whenever you write a value that is aggregated over. The documentation shows an example of calculating a moving average this way, which requires no query at all, and thus scales to any sized data set.

在这种情况下,您必须牢记,Firestore仅限于每个文档每秒执行一次写入操作.因此,如果您收到的数据更多,则可能需要分发汇总查询,如

In this scenario you'll have to keep in mind that Firestore is limited to performing roughly one write per document per second. So if your receiving more data than that, you may need to distribute your aggregation query as shown in the documentation on distributed counters.

使用另一个数据库进行汇总查询

Use another database for your aggregation queries

另一种选择是使用Firestore存储客户端读取的数据,但使用另一个数据库进行复杂的动态查询.

Another alternative is to use Firestore for storing the data that the clients read, but using another database for the complex, dynamic queries.

典型的示例是将数据从Firestore导出到BigQuery,然后在BigQuery中执行计算,然后将结果写回Firestore,以便客户端可以读取它们.在这里,您将两种产品都发挥出最大的优势:Firestore用于大规模提供数据,BigQuery用于大规模处理数据.

A typical example of this is to export the data from Firestore to BigQuery, then perform the calculations in BigQuery, and write the results back to Firestore so that the clients can read them. Here you're using both product for what they're best at: Firestore for serving data at scale, and BigQuery for processing data at scale.

这篇关于是否可以在Firestore中运行聚合查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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