【问题标题】:Firebase querying WHEREFirebase 查询 WHERE
【发布时间】:2019-06-13 15:43:06
【问题描述】:

我正在研究下一个项目使用什么后端。 Firebase 目前处于最前沿。

但是,使用 Firebase 进行查询时,您似乎无法执行传统的 SQL 查询,因为它是 JSON 数据库?如 WHERE foo... 似乎仅限于 EQUALS。

如果我想做 WHERE foo 小于或大于之类的操作,难道我必须抓取所有数据并在本地过滤吗?

还是因为 JSON 比 SQL 数据库轻得多,所以只获取大块分页数据然后在本地过滤更可行?

【问题讨论】:

  • 还有其他查询数据的选项 - 您能否提供您的 Firebase 结构的 sn-p(请提供文本)和用例?使用这些数据点直接解决问题会更容易。

标签: ios objective-c filter firebase firebase-realtime-database


【解决方案1】:

除了 Where 子句,您可以使用多种方法来过滤 Firebase 数据...

你检查latest Doc.,过滤数据部分

过滤数据

要过滤数据,您可以在构造查询时将任何限制或范围方法与 order-by 方法结合使用。

Method               |  Usage
------------------------------------------------------------------
queryLimitedToFirst     Sets the maximum number of items to return from the beginning of the ordered list of results.
queryLimitedToLast      Sets the maximum number of items to return from the end of the ordered list of results.
queryStartingAtValue    Return items greater than or equal to the specified key, value, or priority, depending on the order-by method chosen.
queryEndingAtValue      Return items less than or equal to the specified key, value, or priority, depending on the order-by method chosen.
queryEqualToValue       Return items equal to the specified key, value, or priority, depending on the order-by method chosen.

与 order-by 方法不同,您可以组合多个限制或范围函数。例如,您可以结合 queryStartingAtValue 和 queryEndingAtValue 方法来将结果限制在指定的值范围内。

【讨论】:

    【解决方案2】:

    是的,在 Firebase 中您不会找到像 where something > somevalue 这样简单的典型 SQL 查询,但它们确实提供了类似于 sql 的基本 where 子句的选项。

    例如

    如果我们有类似where height > 3的查询

    我们在 Firebase 中这样做

     FQuery *queryRef = [[ref queryOrderedByChild:@"height"] queryStartingAtValue:@3];
    

    旧版文档非常详细地解释了这一点 https://www.firebase.com/docs/ios/guide/retrieving-data.html

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 2021-06-17
      • 2021-11-13
      • 2021-09-26
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      相关资源
      最近更新 更多