我有以下Firebase
JSON
数据结构;
{
"USERS": {
"YjGJsvCb58OsTThv6JLmK1dMuHr1": {
"Email": "test@test.com"
},
"zQpb7o18VzYsSoTQtT9DNhOqTUn2": {
"Email": "test2@test.com"
}
},
"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
并获取POSTS
作为duration
或用户选择的所有Monthly
。如您所见,每个POST
都分配了AutoID
。
我要找的结果应吐出来:
{
"-KSe4q1DXoulArzWm_Zm": {
"addedByUser": "YjGJsvCb58OsTThv6JLmK1dMuHr1",
"content": "Post 2",
"cost": "550",
"duration": "Monthly",
"latitude": "51.5100199999855",
"longitude": "-0.133507191218496",
"timestamp": "Tue 27 Sep"
}
}
但是,对于我的生活,我不能搜索AutoID
。我已经尽力了queryFor
& observe
方法。我搜索了StackOverflow,似乎没有解决方案。我是否正确构建了数据?我需要AutoID
的原因是某些帖子可能是相同的。
非常感谢任何帮助。
答案 0 :(得分: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.
}
})