Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

swift - How to query nested data in Firebase Database?

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.

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

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}

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"

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}

From Firebase Documentation

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")
   }
})

EDIT 2

Initial Structure looks like this:

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

Modified structure:

{
 "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"
      }
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...