FIRDatabaseQuery:如何进行内部连接

我正在尝试在FIRDatabaseQuery对象上进行内部联接。

下面是数据库结构。 我有一些链接到评论后的post。 我正在尝试获取特定用户添加评论的所有post:

{ "posts" : { "-KIycKhZU55dmKnHbbxv" : { "author" : "John Doe", "body" : "This is a post test", "title" : "test", "uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2" }, "-KIyg_ks1O5QL4_4dfq_" : { "author" : "Jane Doe", "body" : "This is a post test", "title" : "test2", "uid" : "x5leSBGArnd10JilD9YDyNBNfZ03" },... } "post-comments" : { "-KIycKhZU55dmKnHbbxv" : { "-KIycMyL0Vy1BHVdI4zc" : { "author" : "toto", "text" : "test", "uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2" }, "-KIyg_ks1O5QL4_4dfq_" : { "author" : "toto", "text" : "test", "uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2" } },... } 

在SQL中,这将被翻译成类似于的内连接查询:

选择*来自post post的内部联接后评论post-comments.uid =“user id”

有人知道如何在firebase中获得类似于内连接的东西吗?

非常感谢Yacine

您将需要使用嵌套的firebase调用。 您可以在此问题中找到一个javascript示例,但您的swift代码应如下所示:

 ref.child("posts").observeEventType(.ChildAdded, withBlock: { (snapshot) in if let postId = snapshot.key as! String { let commentsRef = ref.child("post-comments") commentsRef.child(postId).queryOrderedByChild("uid").queryEqualToValue(userId).observeSingleEventOfType(.Value, withBlock: { (snapshot) in for child in snapshot.children.allObjects as [FDataSnapshot] { print(child.value) } }) { (error) in print(error.localizedDescription) } } }) 

你可能想考虑一下

 "posts" : { "-KIycKhZU55dmKnHbbxv" : { "author" : "John Doe", "body" : "This is a post test", "title" : "test", "uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2" "commented_by" "SHaH36BLwgPvwgi9cDmRnsnONFB2": true 

然后你可以简单地查询

 posts.child("commented_by/uid").queryEqualToValue(true) 

或者,更改后评论以更好地匹配您要执行的查询:

 "post-comments" : { "-KIycMyL0Vy1BHVdI4zc" : { "author" : "toto", "post_data": post_id: "-KIycKhZU55dmKnHbbxv", post_title: "test" "text" : "test", "uid" : "SHaH36BLwgPvwgi9cDmRnsnONFB2" }, 

这使查询成为一个快照,因为可以查询后注释节点的uid,它返回他们评论过的post的所有post_id。 它不会返回post本身,但是,您可能只是在寻找标题,以便您可以填充列表。 用户点击它后点击它就可以检索实际数据。