Is it possible to run aggregation queries in Firestore?

后端 未结 1 2040
刺人心
刺人心 2020-12-21 23:30

Stack:

Ionic
Nodejs/Express
Cloud Firestore

I am tasked with writing an app that takes dates in \"day\" format, with a balance for that day

相关标签:
1条回答
  • 2020-12-21 23:44

    From the docs you linked:

    Cloud Firestore does not support native aggregation queries.

    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.

    The common solutions are to:

    1. Run the aggregations on the client

      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.

    2. 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.

      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.

    3. Use another database for your aggregation queries

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

      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.

    0 讨论(0)
提交回复
热议问题