问题
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:
- Retrieve all data matching your query and count client-side
- 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