首页 文章

Firebase复杂查询子句

提问于
浏览
2

如果我在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投票的所有图像?

我阅读了文档,据我了解,这些类型的查询根本不可能 . 所以,如果这是不可能的,我怎么能尝试重新构建我的数据,以便能够在我给出的例子中进行类似的查询?

我使用的是“新”Firebase控制台,目前是Firebase SDK的iOS和Android版本,但我不需要特定的示例,而是希望了解如何在Firebase中进行复杂查询,构建我的数据,或者,如果我误解了文档,通过正常的查询 .

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

1 回答

  • 4

    是的,你可以这样做 .

    这是一个查找没有john投票的节点的示例,它将从问题中的结构返回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返回多个节点,因此我们需要迭代,然后使用for child ...来获取每个节点 .

    作为旁注,如果这些是Firebase用户,则应使用userId而不是人名 . 尽可能将动态数据(例如人名)与其密钥分离,并参考密钥 . 约翰可能希望将来被称为约翰尼 .

相关问题