bibscy
bibscy

Reputation: 2708

How to sort and filter data in Firebase?

I want to make a query in order to get all children under bookings node that contain the the child key TimeStampDateAndTime and the value of the child is between 1519912278 and 1520689878. For some reason it returns null even though the value of the key TimeStampDateAndTime in the JSON object shown below is in this range 1519912278 - 1520689878.

let ref = dbRef.child("Cleaners").child(self.uidOfTextField!).child("bookings").queryOrdered(byChild: "TimeStampDateAndTime").queryStarting(atValue: 1519912278).queryEnding(atValue: 1520689878)

finalRef.observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in         
    print("snapshot.value is  line 81 is \(snapshot.value)") //prints (<null>)
}

"Cleaners" : {
    "05MSPgkP1ddhFqXDRjIB4npGEPV2" : {
        "bookings" : {
             "392239680" : {
                  "BookingAmount" : “10”,
                  "BookingCompleted" : "false",
                  "TimeStampDateAndTime" : "1520526600",
           },
       }
   },
 }

Upvotes: 0

Views: 273

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80904

It gives you null because it does not exists, you need to do something like this:

queryOrdered(byChild: "TimeStampDateAndTime").queryStarting(atValue: 152).queryEnding(atValue: 152\uf8ff)

this will give you all TimeStampDateAndTime that have 152 at the beginning

Upvotes: 1

Toma Radu-Petrescu
Toma Radu-Petrescu

Reputation: 2262

"TimeStampDateAndTime" is a string in your case, so ordering by it won't give the expected results. You can change its type to Int.

Upvotes: 1

Related Questions