像MySQL一样从Firebase检索计数数据 [英] Retrieve count data from Firebase like MySQL

查看:36
本文介绍了像MySQL一样从Firebase检索计数数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试从MySQL数据库中提取数据时,我可以做类似的事情:

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

选择*来自用户的订单ID 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.

Firebase是否有类似的东西?我只能从一个用户检索特定数据?

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

我的firebase数据库是这样的:用户->用户信息(名称,年龄,车辆等.)

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?

我尝试了以下操作,但是我尝试执行的操作未成功,因为在 users 之后,users id是下一个孩子.有没有办法查询过去的情况?

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

推荐答案

Firebase数据库中没有计数查询(也没有其他聚合查询).您的选择是:

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

  1. 检索与查询匹配的所有数据并统计客户端数量
  2. 保留一个单独的计数节点,该计数节点在添加/删除项目时进行更新.

对于#2,您可能会发现使用Cloud Functions较为方便,为此使用

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

另请参阅:

这篇关于像MySQL一样从Firebase检索计数数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆