所以我有以下firebase数据结构:
Games
--- 20190220000_NCAA (SportID)
--- WeekId : 1
--- etc...
--- 20190221000_NCAA
--- WeekId : 2
--- etc...
--- 20190204000_NFL
--- WeekId : 1
--- etc...
--- etc...(SportsID)
所以我想要达到的目标是:获得与周= 1相关的所有NCAA游戏
根据上面的示例,只应返回20190220000_NCAA。
我的查询如下:
gamesRef.queryOrderedByKey().queryStarting(atValue: "_NCAA").queryEnding(atValue: "_NCAA\u{f8ff}").queryOrdered(byChild: "WeekId").queryEqual(toValue: "1").observe(.value, with: { snapshot in
因此,如果我正确理解它,应该以_NCAA作为子字符串查询所有'Keys',然后按Week id = 1排序结果,并以'_NCAA'作为子字符串?
问题在于它没有返回正确的结果。
有人对我如何实现查询有任何见识吗?
答案 0 :(得分:1)
您显示的代码不应进行事件编译。 Firebase数据库查询只能包含一个orderBy...
子句,并且不能进行后缀匹配(您的_NCAA
在键的末尾)。
要允许您使用,您应该有一个包含星期编号和org(NCAA
/ NFL
)值的键/属性。例如。
Games
--- 20190220000_NCAA (SportID)
--- WeekId : 1
--- WeekOrg : "01_NCAA"
--- etc...
--- 20190221000_NCAA
--- WeekId : 2
--- WeekOrg : "02_NCAA"
--- etc...
--- 20190204000_NFL
--- WeekId : 1
--- WeekOrg : "01_NFL"
--- etc...
--- etc...(SportsID)
有了这个,您可以在第1周获得NCAA的节点,
gamesRef.queryOrdered(byChild: "WeekOrg").queryEqual(toValue: "01_NCAA").observe(.value, with: { snapshot in
也请在这里查看我的答案:http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase