Firebase 用于复杂查询.不行吗? [英] Firebase for complex query. A no go?

查看:22
本文介绍了Firebase 用于复杂查询.不行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的新项目,我已经从 parse-server 转移到了 firebase,但在项目中我开始认为这是一个坏主意.

I've moved from parse-server to firebase for my new project, but reached a point in the project where I beginning to think it was a bad idea.

基本上,我正在制作一个应用程序,人们可以在其中发布有关他们镇上正在进行的音乐会的信息.

Basically, I'm making an app where people can post information about concerts going on in their town.

我的第一个挑战是过滤事件,因此用户只能获取他/她所在城镇的事件.我通过在城市之后构建数据来做到这一点:

My first challenge was to filter the events, so a user only get events in his/her own town. I did this by structure the data after cities:

{
    concerts: {
        "New york": {
            ...,
            ...
        }, 
        "Chicago": {
            ...,
            ...
        }
    }
}

然后我想我需要另一个过滤器来处理音乐会的类型,例如摇滚、流行等.所以我虽然我做了另一个重组.但是,可能还需要 5-10 个过滤器,并且很难以良好的方式构建数据库.

Then I figure I need another filter for the type of concert, e.g rock, pop, etc. So I though I did another restructure. However, there probably need to be 5-10 more filters, and it will become very hard to structure the database in a good way.

我想过多次查询,但这是不允许的:

firebase.database().ref("concerts")
.orderByChild("type").equalTo("rock")
.orderByChild("length").equalTo("2")
.orderByChild("artist").equalTo("beatles")

我想过从服务器获取所有内容,然后在客户端过滤结果.然而,我发现这有两个问题:

I thought about fetching everything from the server, and then filter the result in the client. I see however two problems with this:

  1. 可能会下载大量不必要的数据.
  2. 某些音乐会仅对某些用户(例如至少参加了 10 场其他音乐会的用户)锁定,并且可能存在安全方面的问题,将这些音乐会拉回给不允许观看的用户.

我考虑结合过滤器来创建查询键,像这样this,但是如果有超过 10 个过滤器,它会变得很复杂.

I thought about combining filters to create query keys, like this this, but with over 10 filters, it will become to complex.

是否有解决方案,或者我应该忘记这个用例的 firebase 吗?

Is there a solution to this or should I forget about firebase for this use case?

提前致谢

推荐答案

在 Firebase 中可以制作极其复杂的查询.数据需要存储在易于查询的结构中,最重要的是,不要害怕重复数据.

Incredibly complex queries can be crafted in Firebase. The data needs to be stored in a structure that lends itself to being queried and most importantly, don't be afraid of duplicate data.

例如,假设我们有一个应用,让用户可以选择特定年份和月份、特定城市和特定流派的音乐会.

For example, lets assume we have an app that enables a user to select a concert for a particular year and month, a specific city, and in a particular genre.

有3个参数

年_月城市流派

UI 首先询问用户选择城市

The UI first queries the user to select a City

Austin

然后 UI 要求选择年份和月份

then the UI asks to select a year and month

201704

然后是一个流派

Rock

您的 Firebase 结构如下所示

Your Firebase structure looks like this

concerts
  concert_00
   city: Memphis
   year_month: 201706
   genre: Country
   city_date_genre: Memphis_201606_Country
  concert_01
   city: Austin
   year_month: 201704
   genre: Rock
   city_date_genre: Austin_201704_Rock
  concert_02
   city: Seattle
   year_month: 201705
   genre: Disco
   city_date_genre: Seattle_201705_Disco

您的 UI 已经向用户查询查询信息,并以此构建查询字符串

Your UI has already polled the user for the query info and with that, build a query string

Austin_201704_Rock

然后查询该字符串的city_date_genre"节点,您就拥有了数据.

and then query the 'city_date_genre' node for that string and you have your data.

如果用户想知道 2017 年 4 月在奥斯汀举行的所有音乐会怎么办

What if the user wanted to know all of the concerts in Austin for April 2017

queryStartingAt("Austin_201704").queryEndingAt("Austin_201704")

您可以通过添加另一个查询节点并更改数据顺序来轻松扩展此功能

You could easily expand on this by adding another query node and changing the order of the data

concerts
  concert_00
   city: Memphis
   year_month: 201706
   genre: Country
   city_date_genre: Memphis_201606_Country
   city_genre_date: Memphis_Country_201606

并且根据用户选择数据的顺序,您可以查询关联的节点.

And depending on which order the user selects their data, you could query the associated node.

添加额外的节点是少量的数据,并允许对您需要的数据进行非常开放的查询.

Adding additional nodes is a tiny amount of data and allows for very open ended queries for the data you need.

这篇关于Firebase 用于复杂查询.不行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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