I am trying to count the number of records in a table in real time without the overhead of loading every single record. To clarify further: I just need to know the number of records not the content.
Asked
Active
Viewed 2.2k times
8
-
1There is currently no method in the Firebase JavaScript API to get the number of children, without getting those children. If you really need this to implement a use-case, you can keep your own count property (using transactions). Also see: http://stackoverflow.com/questions/11461937/firebase-better-way-of-getting-total-number-of-records?rq=1, http://stackoverflow.com/questions/11618032/total-number-of-records-in-firebase-when-am-i-done-counting?rq=1 and http://stackoverflow.com/questions/16239819/performance-of-firebase-with-large-data-sets?rq=1 – Frank van Puffelen Jan 03 '15 at 02:39
-
6Possible duplicate of [In Firebase, is there a way to get the number of children of a node without loading all the node data?](http://stackoverflow.com/questions/15148803/in-firebase-is-there-a-way-to-get-the-number-of-children-of-a-node-without-load) – Adarsh Madrecha Jul 08 '16 at 09:47
-
1Besides duplicated question, you clearly noted that you don't want a solution with the overhead, yet you accepted an answer with the overhead. – vucalur Sep 04 '16 at 15:04
2 Answers
2
ref.once("value", function(snapshot) {
console.log("Count!", snapshot.numChildren());
});

Adrian Lopez Gomez
- 57
- 3
-
2Please, feel free to expand upon your answer. More specifically explain briefly what your proposed solution does, why it solves the problem, and in this case also how it differs from the heavily down-voted near-identical answer already given. – Xyz Oct 01 '20 at 09:13
-
Please provide explanation for your answer. It is important that you explain how does your solution work so that readers understand the answer. – Shubham Kadlag Oct 01 '20 at 09:31
-6
I think you're looking for numChildren(), here's the docs from the Firebase APi.
https://www.firebase.com/docs/web/api/datasnapshot/numchildren.html

Alex J
- 1,029
- 6
- 8
-
6Since this method is defined on `DataSnapshot`, it will only be available when all the children are loaded. – Frank van Puffelen Jan 03 '15 at 02:36
-
@FrankvanPuffelen yeah that's correct. I have added a code example in the answer. Thanks. – Paul Jan 03 '15 at 02:42
-
@FrankvanPuffelen are you sure that the dataSnapshot contains all the child elements? – Paul Jan 03 '15 at 02:44
-
1That depends on how you get that `DataSnapshot`. But to be able to iterate over (or count) the children, it must be coming from `on('value`, which will retrieve all children. If that's not he behavior you see, share your code snippet (fiddle/bin/plunkr). – Frank van Puffelen Jan 03 '15 at 12:49
-
If you just want to show stats, without downloading the entire tree, I was thinking about doing this on the server, then pushing the count to the Firebase DB, so client just listens to a node in the DB for count. However, this would require a separate server, and all the bandwidth for the server to d/l the data set to tally up the count, and if you want near real time you have to run the server code every few seconds; dealing with waiting for download to finish before restarting server counting. So not a simple solution. – Giant Elk Jun 17 '16 at 03:31
-
2Could you please tell me how to do the same in new firebase.? – Pruthvi Hariharan Jun 20 '16 at 22:17
-
@PruthviHariharan define "new firebase". You surely mean Firebase 3.X, but still people have to look at your comment date, look at Firebase release calendar, … you get the point. Still, there are differences between, say, Firebase 3.0 and 3.3 – vucalur Sep 04 '16 at 15:01