如果我在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的iOS和Android版本,但我不需要具体的示例,相反,我想了解如何在Firebase中进行复杂的查询,或者通过重新构造我的数据,或者,如果我误解了文档,通过正常的查询。
在客户端过滤数据的最大问题是,可能会有数百万张图像,而所有这些图像的检索都会非常糟糕。
发布于 2016-06-14 17:13:55
是的,你能做到的。
这里有一个示例来查找没有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子节点.去找每一个人。
另外,如果这些用户是Firebase用户,则应该使用userId而不是人员名称。尽可能地将动态数据(例如人名)与其键分离,并引用该键。约翰将来可能想被称为强尼。
https://stackoverflow.com/questions/37817585
复制相似问题