angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.64k stars 2.2k forks source link

count, sum and average #3462

Open rubenheymans opened 8 months ago

rubenheymans commented 8 months ago

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

google-oss-bot commented 8 months ago

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

Sapython commented 6 months ago

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());

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.

Muthu0706 commented 3 months ago

Good