4

I'm trying to get a timestamp ordered list of elements out of Firebase, using Golang and Firego.

The documentation suggests:

var v map[string]interface{}
if err := f.StartAt("a").EndAt("c").LimitToFirst(8).OrderBy("field").Value(&v); err != nil {
    log.Fatal(err)
}
fmt.Printf("%s\n", v)

I must be missing something completely obvious, but isn't v going to be unordered? When I loop through the map (for key, val := range v) the values won't be in the same order as they have been sent in the response of the call to Firebase, since the order of access is undefined.

What am I missing? Thanks

AL.
  • 36,815
  • 10
  • 142
  • 281

1 Answers1

1

The map of results will be unordered since it is a map, but the original results (limited to top 8) will be ordered before the limit, so the order could be very important.

I agree a map is a bad type for results of this kind, they're probably using that because results come back as json (though that does have an order, unlike go's map). They should be returning an array of results to preserve order.

Kenny Grant
  • 9,360
  • 2
  • 33
  • 47
  • Ok so I'm not mad.. I'm surprised there is nobody that has cared about ordering until now. I will add an issue to the project then. – supercalifragilistichespirali Mar 16 '17 at 21:40
  • "but the original results (limited to top 8) will be ordered" --- would they be returned as a JSON array or JSON object though? – zerkms Mar 16 '17 at 23:19
  • 1
    *firebaser here* Firego is built on Firebase's REST API, which has the same limitation. Since it exposes a JSON object, the results are inherently unordered. Therefor the only reason to use `OrderBy` is to filter the data. See [my answer here](http://stackoverflow.com/questions/31808990/firebase-returning-keys-of-child-node-in-different-orders-on-different-devices-a/31812506#31812506). The Firebase SDKs return a more complex data structure that allows it to expose the children in the correct order on the client. – Frank van Puffelen Mar 16 '17 at 23:47
  • 1
    Yes, I tried to use the REST APIs directly too and I saw that instead of an array, it would return a JSON object, which confused me greatly. Although, the JSON object seems to be returned always with the same sequence of elements. Can I rely on that? (was thinking of doing some string replacement to convert the REST API JSON object response into an array and then finally use the GO json.Unmarshall functionality. Also, is there any plan to fix the REST APIs? This seems to be a very big limitation. – supercalifragilistichespirali Mar 17 '17 at 07:01
  • Actually, I guess what I need to do is apply orderBy and other query filters like startAt etc and then in code sort the result myself. Firebase will still return the correct paginated content, is just that I cannot rely on the order within the specific response, but since the result will contain the field I'm ordering by, I can sort the view myself. Thanks – supercalifragilistichespirali Mar 17 '17 at 08:33
  • yes. Weird they return their data like that... it might not matter for many of their users. – Kenny Grant Mar 17 '17 at 08:35