问题描述
我是第一次使用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:
- 查询客户端中的所有文档,并对其进行手动汇总,或者
- 随着每个新分数的增加,随着时间的流逝保持总计.然后,您可以在需要时在另一个文档中查询该运行总计.
这篇关于如何在Firestore集合上执行聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!