0

When trying to pull data from a MySQL database, I can do something like:

SELECT * FROM users ORDER BY id WHERE vehicle = car

That should get me all the users that drives a car and not show users that drives a motorcycle for instance.

Is there something like this for Firebase? I can only retrieve specific data from one user?

My firebase database is like this: user -> user info (name, age, vehicle etc..)

I want to query every user that drives a car and display them in a row. How do I do that?


I have tried the following, but I didn't succeed with what I tried to do, since after users the users id is the next child. Is there a way to query past that?

var recents = firebase.database().child('users').orderByChild('department').equalTo(department);
            recents.on('child_added', function(snapshot) {
            var countOfUserInDepartment = snapshot.count;
            
                document.querySelector("#cphCount").innerHTML = countOfUserInDepartment;
            
            });

0

1 Answer 1

2

There are no count queries (nor other aggregation queries) in the Firebase Database. Your options are:

  1. Retrieve all data matching your query and count client-side
  2. Keep a separate count-node that you update whenever you add/remove items.

For #2 you may find it convenient to use Cloud Functions, for which there an an example of keeping such a counter.

Also see:

Sign up to request clarification or add additional context in comments.

3 Comments

You use the standard Firebase methods for reading a list of data or querying a list of data.
I have edited my question. How can I query past a child that I "don't know what is"?
I reopened the question, but have no idea why you mention count in your title. You might want to update that, so that hopefully somebody will be able to help better..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.