1

This is the structure of my data:

ft-public-records
   2015-01-01 00:00:00 GMT
        addedByUser: "user@gmail.com"
        rating: 1
        time: "2015-01-01 00:00:00 GMT"
        timestamp: 1490437551.593684

Without queryStartAt, I can get the object without problems:

self.publicRef.child("2015-01-01 00:00:00 GMT").queryOrdered(byChild: "rating").observe(FIRDataEventType.value, with: { snapshot in
      print(snapshot)
})

Result:

Snap (2015-01-01 00:00:00 GMT) {
    addedByUser = "user@gmail.com";
    rating = 1;
    time = "2015-01-01 00:00:00 GMT";
    timestamp = "1490437551.593684";
}

But the moment I add the queryStarting at 1 for ratings, I get null.

self.publicRef.child("2015-01-01 00:00:00 GMT").queryOrdered(byChild: "rating").queryStarting(atValue: 1).observe(FIRDataEventType.value, with: { snapshot in
                print(snapshot)
})

Result:

Snap (2015-01-01 00:00:00 GMT) <null>

What am I missing please?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Houman
  • 64,245
  • 87
  • 278
  • 460
  • Added an answer, let me knw if tht works. thanks. – Badhrinath Canessane Mar 25 '17 at 12:22
  • Can you share a minimal, representative real JSON sample? Right now there's only one node, which makes it hard to see how the query is supposed to work. It's also best to have the real JSON (as text, no screenshot). You can easily get this by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). – Frank van Puffelen Mar 25 '17 at 14:50
  • These are really not properly formatted or used queries. In general, a query would be used to read an unknown node based on a child's value. For the query in this question, you already know the key so a query is not needed - that node can be retrieved using keyRef.observeSingleEvent(of: .value. Perhaps I don't understand what you are trying to do? It would make more sense to query the *ft-public-records* for all children where the rating equals 1 – Jay Mar 26 '17 at 13:23

1 Answers1

1

I ended up restructuring the json by adding another child houmie-2013-01-01 00:00:00 UTC to the 2013-01-01 00:00:00 UTC

{
  "ft-public-records" : {
    "2013-01-01 00:00:00 UTC" : {
      "houmie-2013-01-01 00:00:00 UTC" : {
        "rating" : 1,
        "time" : "2013-01-01 00:00:00 UTC",
        "timestamp" : 1.490550817827253E9,
        "username" : "houmie"
      }
    }
  },
}

This works for me now:

self.ref.child("ft-public-records").child("2013-01-01 00:00:00 GMT").queryOrdered(byChild: "rating").queryStarting(atValue: 1).observe(FIRDataEventType.value, with: { snapshot in
    print(snapshot)
})
Houman
  • 64,245
  • 87
  • 278
  • 460
  • This is really the wrong direction. You don't want to make nodes that deep as it will *prevent* queries. As I mentioned in a comment, you don't need to query for a node when you know the key to the node - you can access it directly! Burying the node one more level deep is going to cause you a world of grief. – Jay Mar 27 '17 at 17:17
  • Thanks Jay, but I need to know all the ratings greater than 1 for a certain day. If I eliminate `"2013-01-01 00:00:00 UTC"` parent, would I be able to query for both `rating` and for `time`? I was under the impression that FireBase wouldn't allow that. – Houman Mar 31 '17 at 22:15
  • Yes, you can definitely do that. There are a number of ways to build that query and bunch of Q&A's here on stackoverflow. See my answer to [This Question](http://stackoverflow.com/questions/42672790/firebase-for-complex-query-a-no-go/42701961#42701961) – Jay Apr 01 '17 at 13:26
  • I see. So your solution is to add a composite key in there such as `time_rating = "2013-01-01 00:00:00 UTC_1"`. The downside to this is that if I needed to query a new combination such as; "give me all records for username houmie on that given day", I wouldn't have that composite key in my data, because I didn't think of that in the first place. But this solution here would allow me to filter for the given day and then query for the username houmie. Does it make sense or am I missing something? Thanks – Houman Apr 01 '17 at 14:44