I\'m trying to set up custom indexes on my firebase data for sorting on certain fields.
Here is my data:
GET https://testindexon.firebaseio.com/.json
<It looks to me like your .indexOn is on the wrong path. You're fetching root, but put the .indexOn under the child path testindexon/
. Thus, your URL would need to be https://testindexon.firebaseio.com/testindexon/.json
in order to take advantage of the orderBy.
According to the Firebase documentation on retrieving data through its REST API:
THE REST API RETURNS UNSORTED RESULTS
JSON interpreters do not enforce any ordering on the result set. While orderBy can be used in combination with startAt, endAt, limitToFirst, or limitToLast to return a subset of the data, the returned results will not be sorted. Therefore, it may be necessary to manually sort the results if ordering is important.
There are indeed samples and others fragments in that documentation that seem to suggest otherwise. But the above statement is true.
Keep in mind that the orderBy
parameter is used for more things than just returning ordered results. You'd also use the method to filter the results, i.e.
GET 'https://testindexon.firebaseio.com/.json?orderBy="age"&equalTo=90'
Or you would order to return a limited number of results:
GET 'https://testindexon.firebaseio.com/.json?orderBy="age"&limitToFirst=10'
So in these cases, the child nodes are sorted on the server and then filtered, but the return value will/may still contain the remaining child nodes in an undetermined order.