Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8884

How to do sum with querying data on firebase

I am trying to do sum with querying data. I am using ionic and firebase. My data is simple like:

vitaly:{count:2,group:1}
kristina:{count:1,group:2}
yuri:{count:1,group:1}
dana:{count:1,group:3}
mark:{count:4,group:1}

is possible to get sum of count by group like: group:1 count:7 .....

Thanks!

Upvotes: 1

Views: 3628

Answers (2)

ali Atwa
ali Atwa

Reputation: 555

newsRef.on("value", function(snapshot) {
   var sum = 0;
   snapshot.forEach(function(childSnapshot) {
   sum += (childSnapshot.val().sth);
});
console.log('NUM '+sum);

Upvotes: 0

Mathew Berg
Mathew Berg

Reputation: 28750

You'll have to do the summation client side but to select your groups you can (depending on you're framework)

 .orderByChild('group').equalTo(1)

Upvotes: 1

Related Questions