angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

count, sum and average

Open rubenheymans opened this issue 2 years ago • 3 comments
trafficstars

how can we use count, sum and average with angularfire? https://firebase.google.com/docs/firestore/query-data/aggregation-queries

rubenheymans avatar Nov 09 '23 18:11 rubenheymans

This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Nov 09 '23 18:11 google-oss-bot

The documentation is already there inside the tab called as Web Modular API please refer to it, whenever checking the docs.

Here a simple implementation with count, sum and average implemented in angular.

import { average, collection, getAggregateFromServer, getCountFromServer, sum } from '@angular/fire/firestore';

const usersCollection = collection(this.firestore, 'users'); // collection of users
const count = await getCountFromServer(usersCollection);  // our query to count number of users (number of documents in a collection)

console.log("count",count.data().count);

const sumQuery = await getAggregateFromServer(usersCollection, {
  totalLikes: sum('likes'),
  totalSubscriptions: average('subscriptions')
}); // query to sum total likes and average total subscriptions
console.log("sumQuery",sumQuery.data());

  • Firebase may throw error on console for a missing index. So just click on the link and wait for little bit and then the index will be created.

Also Just a suggestion

Please, when you are upto a discussion go and talk in the discussions channel. Raising an issue for a doubt or exemplary solution isn't a really great idea. Issues are there to solve problems and bugs for the library itself.

Close this issue if this solution works for you. And for any further things or doubts go on to discussions panel and continue there.

Sapython avatar Dec 22 '23 13:12 Sapython

Good

Muthu0706 avatar Mar 28 '24 08:03 Muthu0706