使用 Firebase 进行数据库式查询 [英] Database-style Queries with Firebase

查看:39
本文介绍了使用 Firebase 进行数据库式查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有使用 Firebase 执行数据库样式查询的快速方法?

Is there a fast way to perform database-style queries with Firebase?

例如:

给定一个带有字段 user_idnameage 的 firebase 参考 users,什么是最好的执行类似这样的查询的方法:

Given a firebase reference users with fields user_id, name, and age, what would be the best way to do a query similar to this:

SELECT name FROM users WHERE `user_id`=147;

SELECT COUNT(*) FROM users WHERE age=21;

推荐答案

一般来说,没有.Firebase 本质上是一个实时数据库",随着数据的变化不断向您流式传输更新,因此进行通用查询更加困难.目前,提供了几个(公认有限的)查询原语.请参阅文档中的查询/限制页面.

In general, no. Firebase is essentially a "realtime database", constantly streaming updates to you as data changes, so it's more difficult to do general-purpose querying. For now, there are a couple of (admittedly limited) querying primitives that are provided. See the Queries/Limits page in the docs.

您通常可以通过多种方法解决这些限制:

You can often work around these limitations through a variety of approaches:

  • 智能地使用位置名称和优先级.如果您将数据结构为/users/[userid]/name,您只需检索/users/147/name 即可完成您的第一个查询".如果您知道要按年龄查询,您可以使用年龄作为用户节点的优先级,然后执行usersRef.startAt(21).endAt(21).on('child_ added', ...)"让所有用户都达到 21 岁.您仍然需要手动统计他们.
  • 进行客户端查询.如果整个数据集很小,您可以检索整个数据集,然后在客户端手动过滤/处理它.
  • 运行单独的服务器.它可以连接到 Firebase,同步数据,然后为客户端回答查询".它仍然可以通过 Firebase 与客户端通信,并且 Firebase 仍然可以作为主要数据存储,但您的单独服务器可以完成快速执行查询的工作.
  • Use location names and priorities intelligently. If you structure your data as /users/[userid]/name, you can accomplish your first "query" by just retrieving /users/147/name. If you know you'll want to query by age, you can use age as the priority for user nodes and then do "usersRef.startAt(21).endAt(21).on('child_added', ...)" to get all users age 21. You still have to count them manually.
  • Do client-side querying. If the entire data set is smallish, you may be able to retrieve the entire data set and then filter / process it manually on the client.
  • Run a separate server. It can connect to Firebase, sync data and then answer "queries" for clients. It can still communicate to clients through Firebase, and Firebase can still be the primary data store, but your separate server can do the work to perform queries quickly.

我们打算随着时间的推移对此进行改进,因为我们意识到与传统关系数据库系统提供的灵活查询相比,这是一个弱点.

We intend to improve on this over time, as we realize it's a weak spot compared to the flexible querying provided by traditional relational database systems.

这篇关于使用 Firebase 进行数据库式查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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