如何在Firestore集合上执行聚合 [英] How to perform aggregation on firestore collection

查看:69
本文介绍了如何在Firestore集合上执行聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用Firestore,并且使用的是我拥有用户和游戏数据的数据集.
我在每个游戏数据中finalScore

I'm working with firestore for the first time and the dataset in which I have users and games data.
In every game data I've finalScore


每个游戏中Games中都有一个名为userID的外键,可以区分该游戏数据所属的


There is a foreign key in Games called userID for each game that differentiate that this game data belong to.

我想在index.js中编写一个函数,该函数可以接受userID并求和所有finalScore并返回该用户的总分.

I want to write a function in index.js which can take in the userID and SUM all the finalScore and return the total score for that user.

到目前为止,我在搜索Google之后所拥有的是:

So far what I have after searching google is:

exports.myFunctionName = functions.firestore
    .document('Users').onWrite((change, context) => {
      // ... Your code here
    });

这是我第一次进行此操作,并且想要实现一些我没有找到相关帮助的东西.我正在开发一个iOS应用,并将其用作后端服务.

This is the first time I'm working on this and want to achieve something for which I didn't find related help. I'm working on an iOS app for which I'm using this as a backend service.

推荐答案

Firestore没有任何聚合查询,因为它们无法按Firestore的要求进行大规模扩展.如果要在文档中查找总价值,则必须:

Firestore doesn't have any aggregation queries, because they don't scale massively as Firestore requires. If you want to find a total value among documents, you will either have to:

  1. 查询客户端中的所有文档,并对其进行手动汇总,或者
  2. 随着每个新分数的增加,随着时间的流逝保持总计.然后,您可以在需要时在另一个文档中查询该运行总计.

这篇关于如何在Firestore集合上执行聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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