数据库的值计数

时间:2018-05-27 15:24:42

标签: swift firebase firebase-realtime-database

我的数据库结构userDatabase/userID/Customers我有两个客户。例如路径:

usersDatabase
    g8voYf1yoChnpmhkoPgtmO4FQT62 - (uid)
        Customers
            Tom Smith (customer with custom ID)
                -LDFZw1tca8KOrnqyyWH - (auto id of customer child)
                    Status of Service: "Open service"
            Ben Thomas (customer with custom ID)
                -LDFZw1tca8KOjgoenBN - (auto id of customer child)
                    Status of Service: "Open service"

可以获取值的数量"开放服务"形成我数据库中的所有客户?现在我只知道,如何为每个客户打印这个值......

我从数据库中获取价值的代码:

let userID = Auth.auth().currentUser?.uid
    let usersDatabaseRef = Database.database().reference().child("usersDatabase").child(userID!).child("Customers")
    usersDatabaseRef.observe(.value, with: { snapshot in
        var totalCustomerCount = 0
        for child in snapshot.children {
            let childSnap = child as! DataSnapshot
            let childrenRef = childSnap
            totalCustomerCount += Int(childrenRef.childrenCount)
            print("user \(childSnap.key) has \(childrenRef.childrenCount) customers")

            let userCustomerSnap = childSnap
            for customer in userCustomerSnap.children.allObjects as! [DataSnapshot] {
                let customerSnap = customer
                let dict = customerSnap.value as! [String: Any]

                let stat = dict["Status of Service"] as! String

                let myStatistic = PrintModel(status: stat)
                self.statistic.append(myStatistic)
                print("Statistic: \(String(describing: myStatistic.status))")
            }
        }
        print("... and there are \(totalCustomerCount) total customers")
    })

例如我的日志现在显示:

  1. 用户Tom Smith有1位客户
  2. 统计:可选("开放服务")
  3. 用户Ben Thomas有1位客户
  4. 统计:可选("开放服务")
  5. 但我想表明:

    1. 统计数据:2

1 个答案:

答案 0 :(得分:0)

构建Firebase数据时,结构通常基于您想要从中获取的内容。在这种情况下,您想要的数据在发布的结构中太深,无法使用。因此,改变结构将使这一点变得轻而易举。

将数据分离到自己的节点中,对数据进行非规范化。喜欢这个

users
   uid_0
     //some  info about this user
     open_service_calls
          auto_id_0: true
          auto_id_1: true
   uid_1
     //info about this user

customers
    customer_id_0
      customer_name: "Tom Smith"
      open_service_calls
          auto_id_0: true
    customer_id_1
      customer_name: "Ben Thomas"
      open_service_calls
          auto_id_1: true

service_calls
   auto_id_0
      customer_id: customer_id_0
      user_id: "uid_0"
      status_of_service: "Open service"
   auto_id_1
      customer_id: customer_id_1
      user_id: "uid_0"
      status_of_service: "Open service"

允许客户,用户进行查询并解决此问题,轻松统计数据库中所有用户和客户的所有Open Service;查询service_calls / status_of_service以获得"打开服务"

它还允许您快速访问所有服务呼叫,为任何用户,任何客户打开或关闭,甚至只为用户的客户打开服务电话。

其他节点将提供进一步的查询灵活性 - 将用户ID存储在客户节点中将允许超级简单的查询来检索特定用户的所有客户,即使他们没有服务呼叫;这一切都取决于你想要从Firebase中获得什么。

---旧答案在下面---

根据我对原始问题的评论,此答案涉及使用深层查询来查询客户子节点的子项。这里的想法是在用户节点内,有一个Customers节点,其中每个子键是一个客户名称,该节点的值是一个键:值对#34; serviceID"然后是一个值,该值可能包含有关该服务的其他子节点。

深度查询的重要部分是保持您查询的子键始终如一地命名 - 在这种情况下,我们使用键' serviceID'所以查询可以正确解析路径,然后可以查询任何子节点:status_of_service,可能是时间戳甚至是服务位置

改变之前的初始结构是

Customers
  Tom Cruise
      serviceID
         status_of_service: "Open service"
         //timestamp of service?
         //location of service?
         //other data about service we may need to query?
  Ben Smith
      serviceID:
         status_of_service: "Open service"

请注意,对于此答案,self.ref = my firebase,因此客户是该路径的直接孩子。

let ref = self.ref.child("Customers")
let query = ref.queryOrdered(byChild: "serviceID/status_of_service")
               .queryEqual(toValue: "Open service")
query.observeSingleEvent(of: .value, with: { (snapshot) in 
    for child in snapshot.children {
        let snap = child as! DataSnapshot
        print(snap)
    }
})

输出是两个客户节点

   tom_smith
      serviceID
        status_of_service: "Open service"
   another_customer
      serviceID
        status_of_service: "Open service"