Chaudhry Talha
Chaudhry Talha

Reputation: 7868

How to perform aggregation on firestore collection

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

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

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.

So far what I have after searching google is:

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

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.

Upvotes: 0

Views: 958

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317427

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. Query all the documents in the client, and sum them manually, or
  2. Keep a running total over time, as each new score is known. Then, you can query for that running total in another document when you need it.

Upvotes: 2

Related Questions