简体   繁体   English

使用Angularfire2从Firestore提取的数据分组

[英]Grouping of data fetched from firestore using Angularfire2

Functionality: 功能:

I have a Angular project for which I have to generate a graph showing invoiceDate vs saleAmount. 我有一个Angular项目,我必须为其生成一个显示invoiceDate vs saleAmount的图形。 The data is fetched from the invoice collection. 从发票收集中获取数据。

var invoiceRef = this.afs.collection<Invoice>('invoices', 
    ref=> ref.orderBy('creationDate','desc'));

Here I am getting the saleAmount and invoiceDate for each invoice. 在这里,我获取每个发票的saleAmount和invoiceDate。

Issue 问题

How to count the saleAmount for each invoice Date? 如何计算每个发票日期的salesAmount?

In traditional SQL you would write something like 在传统的SQL中,您将编写类似

SELECT COUNT(saleAmount) FROM invoice GROUP BY invoiceDate;

How to do the same using angularfire queries for firestore? 使用angularfire查询进行Firestore时该如何做?

Not sure it can be done directly from a firestore query, but you can implement a function that does this after you get the data (or before, with cloud functions ). 不确定可以直接从firestore查询中完成此操作,但是您可以实现一个在获取数据后(或之前使用cloud函数 )执行此操作的函数

It could look like this: 它可能看起来像这样:

const groupedInovices = {};
invocies.forEach((inovice) => {
      groupedInovices[invoice.date] = groupedInovices[invoice.date] ? groupedInovices[invoice.date]++ : 1;
    });

You will end up with an object that looks like this: 您将得到一个看起来像这样的对象:

{
  "05/30/2019": 20
  "05/29/2019: 13
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM