如何在一个查询Firebase中检索多个数据

我是新的firebase和nosql。 我按照指导来构build数据,这里是我的数据看起来像

{"Guardians" : { "jojo-pti-gros" : { "-KBT2ui6wlC6Fgk1c7Xa" : { "creationDate" : "2016-02-26 15:50:56", "level" : "2", "light" : "2", "type" : "Titan" } } }, "Players" : { "jojo-pti-gros" : { "console" : "PS4", "creationDate" : "2016-02-26 15:50:39", "currentGuardian" : "-KBT2ui6wlC6Fgk1c7Xa", "email" : "toto@gmail.com", "gamertag" : "jojo-pti-gros", "guardians" : { "-KBT2ui6wlC6Fgk1c7Xa" : "true" } } }, "Teams" : { "-KBhfH9U3CxQPZiCs5ru" : { "activity" : "Raid - Normal - Crota's end", "creationDate" : "2016-02-29 16:37:14", "language" : "Any language", "leader" : "jojo-pti-gros", "maxPlayers" : "6", "mic" : "true", "minimumLight" : "55", "players" : { "jojo-pti-gros" : "true" }, "teamDescription" : "Chaud pour un raid" } }} 

当我试图找回一个团队,我想有一个像这样的JSON。

 "-KBhfH9U3CxQPZiCs5ru" = { activity = "Raid - Normal - Crota's end", creationDate = "2016-02-29 16:37:14", language = "Any language", leader = "jojo-pti-gros", maxPlayers = 6, mic = true, minimumLight = 55, players = { "jojo-pti-gros" = { "creationDate" = "2016-02-26 15:50:56", "level" = "2", "light" = "2", "type" = "Titan" } }, teamDescription = "Chaud pour un raid" } 

但是我有这个json

 "-KBhfH9U3CxQPZiCs5ru" = { activity = "Raid - Normal - Crota's end", creationDate = "2016-02-29 16:37:14", language = "Any language", leader = "jojo-pti-gros", maxPlayers = 6, mic = true, minimumLight = 55, players = { "jojo-pti-gros" = true }, teamDescription = "Chaud pour un raid" } 

我的代码现在

  let fb = Firebase(url:myUrl) fb.childByAppendingPath("/Teams").observeSingleEventOfType(.Value, withBlock: { teams in var teamsArray: [Team] = [] if (teams.value is NSNull) { print("no teams found") } else { print(teams.value) for team in teams.value as! NSDictionary { let newTeam = Team() newTeam.dictionaryToTeam(team.value as! NSDictionary) newTeam.id = String(team.key) for player in newTeam.playerDictionary as NSDictionary { GuardianAPI.sharedInstance.getGuardianByGamertagAndID(player.key as! String, id: player.value as! String, handler: {newGuardian in newTeam.playerDictionary.setValue(newGuardian, forKey: player.key as! String) }) } teamsArray.append(newTeam) } handler(true) } }) 

我不知道如何使用SQL等关键字进行查询。 有人可以知道如何解决这个问题吗?

有几个方向去与此。

一种select是读取你的团队节点,将玩家参考分离成一个数组,然后迭代数组以获得每个玩家参考,并为每个玩家装载一个单独的Firebase调用

一个更好的select是在每个玩家的节点中存储一个参考

  "Players" : { "jojo-pti-gros" : { team: "-KBhfH9U3CxQPZiCs5ru" } 

观察团队节点加载团队数据,然后查询玩家节点中所有有孩子的玩家:team =“-KBhfH9U3CxQPZiCs5ru”

 Firebase *playersRef = [myRootRef childByAppendingPath:@"Players"]; FQuery *query1 = [playersRef queryOrderedByChild:@"team"]; FQuery *query2 = [query1 queryEqualToValue:@"-KBhfH9U3CxQPZiCs5ru"]; [query2 observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { NSLog(@"key: %@ %@", snapshot.key, snapshot.value); }]; 

结果将是所有属于球队的球员的快照。