查找具有特定属性的用户?

时间:2016-05-13 16:43:03

标签: firebase

users
|
|_ john_doe199
|  |
|  |_ subscriptions
|    |
|    |__ service_id1 : true
|
|_ jane_doe123
   |
   |_ subscriptions
     |
     |__ service_id1 : true
     |__ service_id2 : true  

根据此架构,如何找到订阅service_id1的用户?

2 个答案:

答案 0 :(得分:2)

这是Eirik回答的替代方案

usersRef.queryOrderedByChild("subscriptions/service_id1").queryEqualToValue(true)
        .observeSingleEventOfType(.Value, withBlock: { snapshot in
    print(snapshot)
})

这称为Firebase Deep Query

答案 1 :(得分:1)

您可以这样做:

let ref = Firebase(url: "https://<your_app>.firebaseio.com/users")
ref.queryOrderedByChild("subscriptions/service_id1")
   .observeEventType(.ChildAdded, withBlock: { snapshot in
    if let subscribed = snapshot.childSnapshotForPath("subscriptions/service_id1")
                        .value as? Bool {
        print("\(snapshot.key) is subscribed to service_id1: \(subscribed)")
    }
})