Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get size of an element/list in Firebase without get it all?

I need to measure the size/length of an element or list in Firebase but I don't need the real content.

Something like firebase.database().ref("users/").once("length" || "size").then(callback)

ps Javascript SDK

like image 420
Rodolfo Paranhos Avatar asked Jul 18 '16 18:07

Rodolfo Paranhos


People also ask

Can you scale with Firebase?

Automatic Scaling Additionally, the API functions of firebase are designed in order to scale linearly with the size of data being synchronized. It handles the scaling operations. Your application will scale from its first user to its first million user without any change in the code.

What is orderByChild in Firebase?

Inherited from Query.orderByChild. Generates a new Query object ordered by the specified child key. Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query is an error. Firebase queries allow you to order your data by any child key on the fly.

How do you get count in Firebase?

Firebase doesn't currently have a way to count children without loading data, but we do plan to add it. UPDATE: Firebase recently released Cloud Functions. With Cloud Functions, you don't need to create your own Server. You can simply write JavaScript functions and upload it to Firebase.


1 Answers

Firebase doesn't have a count operator, so the only way is to download all children or keep a separate <children>_count property in sync. The latter is not a trivial task (see my answer here for one approach and this example Cloud Function), so most often developers likely end up going with the downloads-too-much-data-but-is-trivial approach:

ref.child("messages").on("value", function(snapshot) {   console.log("There are "+snapshot.numChildren()+" messages"); }) 

A more efficient way to count the children would be to fire a REST call with shallow=true parameter, which will give you just the keys. See In Firebase, is there a way to get the number of children of a node without loading all the node data?

like image 163
Frank van Puffelen Avatar answered Sep 29 '22 15:09

Frank van Puffelen