How do I count number of keys, with the help of firebase functions, in above case there are 3. I am using firebase real time database
Asked
Active
Viewed 2,465 times
4
-
That looks like Firestore to me – Dharmaraj Sep 06 '21 at 14:16
-
1nope real time db – Roy Sougat Sep 06 '21 at 14:19
2 Answers
10
There is no separate count operation for Firebase Realtime Database. You'll have to download the entire snapshot of the parent node (geoTag
) and then count the number of children in your application code.
How to do this, depends on the platform:
- On Android, call
snapshot.getChildrenCount()
. - On iOS, read the
snapshot.childrenCount
property. - In JavaScript, read the
snapshot.size
property
If you have many children and all you need is the count, the above quickly becomes overly wasteful. In such cases, it's best to store the child count in a separate property yourself, and update it whenever you add/remove child nodes. Then you can read only the counter when you need that. There is even a sample Cloud Function that does this.

Frank van Puffelen
- 565,676
- 79
- 828
- 807
0
Supposing that you have geoTag
object locally to your application, you could just call:
Object.keys(geoTag).length

Giovanni Esposito
- 10,696
- 1
- 14
- 30