如何在Firebase数据库中查询嵌套数据? [英] How to query nested data in Firebase Database?

查看:116
本文介绍了如何在Firebase数据库中查询嵌套数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何查询以获取所有时间戳记在1519912278 ... 1520689878之间的所有用户的所有预订?在Firebase文档中,它声明我可以查询深度嵌套的子级.

How can I make a query to get all bookings of all users that have timestamp in the range of 1519912278 ...1520689878 ? In Firebase docs it states that I can query deeply nested children.

根据此答案,它应该可以运行 https://stackoverflow.com/a/47805607 ,但是它返回null.

According to this answer, it should work https://stackoverflow.com/a/47805607 , but it return null.

我的用户应用程序的流程是什么? :
1.用户注册
2.进行预订时,将其保存在/Users/UID/stripe_customer_created_with_card/bookingNumber {booking object}

What is the flow of my User app? :
1. User singn up
2. When booking is made, it is saved at /Users/UID/stripe_customer_created_with_card/bookingNumber{booking object}

为什么我需要/Users下的所有预订?:
还有一个专门为工作人员设计的应用程序,它可以观察/Users节点,每个工作人员都应该能够基于timeStamp从/Users获得预订.例如,worker1382可以读取/Users从2018年3月16日到2018年3月24日所做的预订.

Why do I need all bookings located under /Users?:
There is a second app designed for Workers which can observe /Users node and each worker should be able to get bookings from /Users based on a timeStamp. For example a worker1382 can read bookings made by /Users from 16 March 2018 to 24 March 2018.

dbRef = FIRDatabase.database().reference().child("Users")
dbRef.queryOrdered(byChild: "TimeStampDateAndTime")
     .queryStarting(atValue: "1519912278")
     .queryEnding(atValue: "1520689878")
     .observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in         
        print(snapshot.value) //returns null
     }

 Users
   UID
    cus_CRC50bSq7rXuHv         //stripe customer id for the user
       617643762                //booking number
         TimeStampDateAndTime: "1521309610"
         Postcode: "E14 9DS"
         BookingNumber:"617643762"


另一个问题:预订对象的父键等于BookingNumber.如果我将父键617643762替换为TimeStampDateAndTime值1521309610,是否可以按时间戳订购预订并最终仅获得在1519912278 ... 1520689878范围内的预订?因此,路径看起来像Users/UID/cus/1521309610: {object goes here}


Another question: The parent key of the booking object is equal to BookingNumber. If I replace the parent key 617643762 with TimeStampDateAndTime value 1521309610, would I be able to order the bookings by time stamp and ultimately get only the ones that are within a certain range 1519912278 ...1520689878? So, the path would look like Users/UID/cus/1521309610: {object goes here}

来自Firebase文档

深度嵌套的孩子也可以命令查询,而不仅仅是下一层的孩子.如果您具有如下这样的深层嵌套数据,这将很有用:

Queries can also be ordered by deeply nested children, rather than only children one level down. This is useful if you have deeply nested data like this:

{
  "lambeosaurus": {
    "dimensions": {
      "height" : 2.1,
      "length" : 12.5,
       "weight": 5000
     }
   },
}

现在要查询高度,我们使用对象的完整路径而不是单个键. 现在,我的问题是,如果我不知道完整路径,该怎么办?

To query the height now, we use the full path to the object rather than a single key. Now, my question is, what can't I do if I don't know the full path?

 let ref = Firebase(url:"https://dinosaur-facts.firebaseio.com/dinosaurs")
 ref.queryOrderedByChild("dimensions/height").observeEventType(.ChildAdded, withBlock: { snapshot in
 if let height = snapshot.value["height"] as? Double {
     println("\(snapshot.key) was \(height) meters tall")
   }
})

编辑2

初始结构如下:

 {
 "Users" : {
    "I50dHN7wPqPJClPfYDG76gXizdD2" : { //-> user?.uid of a logged in user 
       "cus_CRC50bSq7rXuHv" : {
         "617643762" : {
             "TimeStampDateAndTime" : "1521309610"
          }
        }
     }
   }
 }

修改后的结构:

{
 "Users" : {
   "I50dHN7wPqPJClPfYDG76gXizdD2" : {
       "1521309610" : { // -> store timeStamp as the immediate child key of UID rather than the booking number
         "TimeStampDateAndTime" : "1521309610",
          "BookingNumber": "617643762"
      }
    }
  }
}


 {
   "UsersProfile":{
     "I50dHN7wPqPJClPfYDG76gXizdD2":{
        "StripeCustomer":"cus_CRC50bSq7rXuHv", // -> store StripeCustomer on the UsersProfile
        "Name": "John Doe"
      }
   }
}

推荐答案

您可以在每个子节点下的已知路径中查询属性.因此,您可以在恐龙样本中查询dimensions/weight,也可以从数据中查询617643762/TimeStampDateAndTime.

You can query properties at a known path under each child node. So you can query dimensions/weight in the dinosaur sample, and you can query 617643762/TimeStampDateAndTime from your data.

但是您无法查询*/TimeStampDateAndTime,因为这不是已知路径.

But you can't query */TimeStampDateAndTime, since that's not a known path.

如果需要获取所有用户在某个日期范围内的预订列表,则需要修改数据结构以允许该查询.例如,通过添加顶级预订列表:

If you need to get a list of bookings in a date range across all users, you will need to modify your data structure to allow that query. For example, by adding a top-level list of bookings:

"Bookings": {
    "book_617643762"
        Customer: "cus_CRC50bSq7rXuHv"
        TimeStampDateAndTime: "1521309610"
        Postcode: "E14 9DS"
}

使用此附加结构,您可以查询:

With this additional structure, you can then query:

dbRef = FIRDatabase.database().reference().child("Bookings")
dbRef.queryOrdered(byChild: "TimeStampDateAndTime")
     .queryStarting(atValue: "1519912278")
     .queryEnding(atValue: "1520689878")
     .observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in         
        print(snapshot.value) //returns null
     }

这篇关于如何在Firebase数据库中查询嵌套数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆