Firebase 复杂查询子句

Firebase complex query clauses

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

{
  "images": {
    "first_image": {
      "url": "https://ima.ge/first_image",
      "uploader": "john",
      "timestamp": "1465920841"
    },
    "second_image": {
      "url": "https://ima.ge/second_image",
      "uploader": "louis",
      "timestamp": "1465920987"
    },
    "third_image": {
      "url": "https://ima.ge/third_image",
      "uploader": "peter",
      "timestamp": "1465920990"
    }
  },

  "votes": {
    "first_image": {
      "john": "up",
      "louis": "down"
      "francis": "up"
    },
    "second_image": {
      "john": "up",
      "louis": "down"
    },
    "third_image": {
      "francis": "up"
    }
  }
}

是否可以查询 john 尚未使用 Firebase 投票的所有图像?

我通读了文档,据我了解,这些查询根本不可能。因此,如果这不可能,我该如何尝试重新构建我的数据,以便能够像我给定的示例中那样进行查询?

我正在使用 "new" Firebase 控制台,目前使用的是 iOS 和 Android 版本的 Firebase SDK,但我不需要具体示例,相反我想了解如何在 Firebase 中进行复杂的查询,通过重新构建我的数据,或者,如果我误解了文档,通过正常查询。

在客户端过滤数据的最大问题是可能有数百万张图片并且检索所有这些图片真的很糟糕...

是的,你可以做到。

这是一个查找没有 john 投票的节点的示例,它将 return 来自问题结构的 third_image 节点。

let ref = self.myRootRef.childByAppendingPath("votes")
ref.queryOrderedByChild("john").queryEqualToValue(nil)
      .observeEventType(.Value, withBlock: { snapshot in
      for child in snapshot.children {
           print(child)
      }
})

记住 .Value returns 多个节点,因此我们需要使用 for child... 遍历然后到达每个节点。

附带说明一下,如果这些是 Firebase 用户,您应该使用 userId 而不是人名。尽可能将动态数据(例如人名)与其键分离并引用该键。 John以后可能要叫Johnny