带有Firebase的数据库式查询 [英] Database-style Queries with Firebase

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

问题描述

使用Firebase执行数据库式查询的方法有哪些?

给定带有字段 user_id name 的firebase引用 users ,和 age ,做什么类似的查询是最好的方法:

  SELECT name FROM users WHERE`user_id` = 147; 

  SELECT COUNT(*)FROM WHERE age = 21; 


解决方案

Firebase本质上是一个实时数据库,随着数据的变化不断向您发送更新,因此进行通用查询更为困难。现在,有一些(被公认是有限的)查询提供的原语。请参阅文档中的查询/限制页面。



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


  • 智能地使用位置名称和优先级。如果将数据构建为/ users / [userid] / name,则只需检索/ users / 147 / name即可完成第一个查询。如果你知道你想按年龄查询,你可以使用年龄作为用户节点的优先级,然后执行usersRef.startAt(21).endAt(21).on('child_added',...)获取所有用户的年龄为21岁。您仍然需要手动计数。

  • 进行客户端查询。如果整个数据集很小,您可以检索整个数据集,然后在客户端上手动过滤/处理它。

  • 运行单独的服务器。它可以连接到Firebase,同步数据,然后回答客户的查询。它仍然可以通过Firebase与客户进行沟通,Firebase仍然可以作为主要的数据存储,但您的独立服务器可以快速执行查询。


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

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

For example:

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;

and

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

解决方案

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:

  • 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天全站免登陆