firestore云函数用于获取集合中的聚合值

问题描述 投票:3回答:1

假设我有一个应用程序存储用户帐单,用户添加或删除帐单。数据结构如下:

users/user_id/bills/bill_id

票据文件结构为

{ bill_name: string, amount: number }

我希望显示与账单名称对应的用户聚合值,可以说我在账单集合中有2个条目

{ bill_name: 'amazon', amount: 1000 }
{ bill_name: 'amazon', amount: 2000 } 

我的输出应该是

{ bill_name: 'amazon', amount: 3000 }

我的问题是什么是最好的聚合值

  1. 创建一个在onWrite /user/user_id/bills/bill_id上触发的云函数,并在用户添加或删除账单时在/users/user_id/aggregated_bills/中创建一个新条目,以及此函数从/users/user_id/aggregated_bills/bill_id/读取数据的位置,其中账单名称为“amazon”add the math to this entry and将新值存储在aggregated_bills集合中。我还想知道我们是否可以从云功能中添加或读取数据库,然后引用它。在functions.firestore.document('/users/{user_id}/bills/{bill_id}').onWrite( ...
  2. 创建一个在HTTPS请求时触发的云功能,并从/users/user_id/bills/读取数据名称为“amazon”的数据并计算其中的聚合值,并返回响应。
  3. 可能是这个问题的任何其他解决方案

在这里,我想说,获取汇总值不仅适用于一个账单,而且适用于多个账单。假设我想向用户仪表板显示他正在看到前20个账单的汇总值

firebase firebase-realtime-database google-cloud-functions angularfire2 google-cloud-firestore
1个回答
5
投票

在这种情况下,onWrite触发器最有意义。考虑将聚合结果写为父文档上的对象/映射,因为这样您只需要一次读取操作来使用数据 - 更快,更便宜。

您的云功能看起来像这样:

exports.aggregateBills = functions.firestore
    .document('user/{user_id}/bills/{bill_id}')
    .onWrite(event => {

    const bill_id = event.params.bill_id; 
    const user_id = event.params.user_id;

    const bill_name = event.data.data().bill_name;

    // ref to the parent document
    const docRef = admin.firestore().collection('users').doc(userId)

    // get all bills and aggregate
    return docRef.collection('bills')
         .where('bill_name', '==', bill_name)
         .get()
         .then(querySnapshot => {
            // get the total comment count

            const bills = []
            // loop over bills to create a plain JS array
            querySnapshot.forEach(doc => {
                bills.push( doc.data() )
            });

            const aggregateData = 'do your calculations here'


            // run update
            return docRef.update({ aggregateData })
         })
});
© www.soinside.com 2019 - 2024. All rights reserved.