【问题标题】:Query value between two other values in Firebase查询 Firebase 中其他两个值之间的值
【发布时间】:2016-06-16 18:44:48
【问题描述】:

如果我在 Firebase 中有以下结构:

{
  "images": {
    "first_image": {
      "url": "https://ima.ge/first_image",
      "uploader": "john",
      "start": "1465920841",
      "end": "1466920841"
    },
    "second_image": {
      "url": "https://ima.ge/second_image",
      "uploader": "louis",
      "start": "1465920987",
      "end": "1476920987"
    },
    "third_image": {
      "url": "https://ima.ge/third_image",
      "uploader": "peter",
      "start": "1465920990",
      "end": "1484920990"
    }
  }
}

是否可以查询当前时间戳在“开始”和“结束”之间的所有图像?因此,如果当前时间戳(自 Unix 纪元以来的毫秒数)为1475920987,它将返回second_imagethird_image,但不会返回first_image,因为时间戳不在开始和结束的范围内。

恐怕这是不可能的,因为在其他 stackoverflow 问题中,用户指出不可能进行多个查询。因此,如果我想做一个投票系统,用户可以决定何时允许其他人投票,是否可以只向用户显示可投票的图像?即使不允许多个查询?

我正在使用 iOS 和 Android Firebase SDK,但不需要特定语言的答案。

【问题讨论】:

  • 几个月前有一个类似的问题,但我现在找不到。我不认为我们在那里找到了解决方案。问题是 Firebase 查询只能按单个属性进行过滤,因此您需要找到一种方法将两个值组合到一个属性中,以允许您正在寻找的过滤。如前所述:我当时想不出,但这次也许有人想出了一个解决方案。
  • 可能不是最优的,但您可以尝试使用嵌套查询进行过滤。例如,最后检索所有内容,然后使用第二个查询对其进行过滤。

标签: swift firebase firebase-realtime-database


【解决方案1】:

您可以在单个查询中同时使用queryStarting(atValue:)queryEnding(atValue:)

所以你需要这样的东西:

let query = Database.database().reference().child("images")
    .queryStarting(atValue:<startTimestamp>)
    .queryEnding(atValue:<endTimestamp>)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2013-09-06
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多