Retrieve count data from Firebase like MySQL

谁说我不能喝 提交于 2020-01-30 12:06:10

问题


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;
            
            });

回答1:


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:

  • Firebase count group by
  • Database-style Queries with Firebase
  • In Firebase, is there a way to get the number of children of a node without loading all the node data?
  • How to get size of an element/list in Firebase without get it all?


来源:https://stackoverflow.com/questions/46367231/retrieve-count-data-from-firebase-like-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!