2016-09-27 139 views
0

我有以下FirebaseJSON數據結構;Firebase數據過濾

{ 
    "USERS": { 
    "YjGJsvCb58OsTThv6JLmK1dMuHr1": { 
     "Email": "[email protected]" 
    }, 
    "zQpb7o18VzYsSoTQtT9DNhOqTUn2": { 
     "Email": "[email protected]" 
    } 
    }, 
    "POSTS": { 
    "-KSe4eJyenccPoC5T0zP": { 
     "addedByUser": "YjGJsvCb58OsTThv6JLmK1dMuHr1", 
     "content": "Post 1", 
     "cost": "600", 
     "duration": "Quarterly", 
     "latitude": "51.5099850000001", 
     "longitude": "-0.133528614416432", 
     "timestamp": "Tue 27 Sep" 
    }, 
    "-KSe4q1DXoulArzWm_Zm": { 
     "addedByUser": "YjGJsvCb58OsTThv6JLmK1dMuHr1", 
     "content": "Post 2", 
     "cost": "550", 
     "duration": "Monthly", 
     "latitude": "51.5100199999855", 
     "longitude": "-0.133507191218496", 
     "timestamp": "Tue 27 Sep" 
    } 
    } 
} 

我想過濾POSTS,並得到所有POSTSdurationMonthly或任何用戶已經選擇了有。正如你可以看到每個POST被分配一個AutoID

結果進出口尋找應該吐出:

{ 
    "-KSe4q1DXoulArzWm_Zm": { 
    "addedByUser": "YjGJsvCb58OsTThv6JLmK1dMuHr1", 
    "content": "Post 2", 
    "cost": "550", 
    "duration": "Monthly", 
    "latitude": "51.5100199999855", 
    "longitude": "-0.133507191218496", 
    "timestamp": "Tue 27 Sep" 
    } 
} 

但我不能爲我的生活,通過AutoID的搜索。我已經嘗試過所有的方法。我已經通過StackOverflow搜索,沒有解決方案似乎工作。我的數據是否正確?我需要AutoID的原因是有些帖子可能是相同的。

任何幫助將不勝感激。

回答

0

試試這個: -

FIRDatabase.database().reference().queryOrdered(byChild: "POSTS").queryEqual(toValue: "monthly").observeSingleEvent(of: .value, with: {(snap) in 


    if let postsDict = snap.value as! [String:AnyObject]{ 

     for each in postsDict { 

      print(each) 

      } 

    }else{ 

    //That username doesnt exists. 

    } 

}) 
+0

接受這個答案,如果它解決您的問題:)編碼愉快 – Dravidian