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;
            
            });
FeReTu
  • 125
  • 2
  • 11

1 Answers1

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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You use the standard Firebase methods for [reading a list of data](https://firebase.google.com/docs/database/web/lists-of-data#reading_and_writing_lists) or [querying a list of data](https://firebase.google.com/docs/database/web/lists-of-data#sorting_and_filtering_data). – Frank van Puffelen Sep 22 '17 at 14:44
  • I have edited my question. How can I query past a child that I "don't know what is"? – FeReTu Sep 22 '17 at 15:14
  • 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.. – Frank van Puffelen Sep 22 '17 at 15:23