0

Solution: As Frank already states, I had to create a separate filed for this sorting to work. As suggested, I created a filed "sortField" which combines the

"clubKey" + "_" + "startDate"

To modify my existing data sets, I made a quick java script function like this:

function createTestDataSet(){
    evd = dbAllEventsListRef;

    evd.on('child_added', function(snap, previous){
        console.log(snap.val());
        data = snap.val();
        data.sortField = data.clubKey + "_" + data.startDate;
        testDiv = document.getElementById("divCarSummary");
        testDiv.innerHTML = testDiv.innerHTML + "<br/>"+data.sortField;
        evd.child(snap.key).set(data);
    });
}

I tested this using the following code:

function testSort(){
    key = "manualClubKey-1";
    testEvents = dbTesting.child('eventList').orderByChild("sortField").startAt(key+"_").endAt(key+"a");
    testEvents.on('child_added', function(snap, previous){
        data = snap.val();
        testDiv = document.getElementById("divCarSummary");
        testDiv.innerHTML = testDiv.innerHTML + "<br/>"+data.startDate+", "+data.name+", "+snap.key+", "+data.clubKey;

        console.log(snap.val());
    });
}

The tricky part was, that the manual for "endAt" states:

The ending point is inclusive, so children with exactly the specified value will be included in the query

But this does not seem to be true. If I give the same value for startAt() and endAt(), I get no results. So I modified the query, that I look for the "clubKey" + the separator. For the Separator I gave a character right after the "_". thus it works like a charm.

I modified my android Code, so every time the startDate, or the clubKey is written, I update the "sortFiled", small change, works like a charm.

Original Question I have some event data. Sample data:

{
  "modeltruckerManualEventID-96" : {
    "canceled" : false,
    "clubKey" : "manualClubKey-1",
    "endTime" : "17:00:00",
    "eventOpenForWeightData" : false,
    "link" : "",
    "locationID" : "manualLocationID-4",
    "name" : "Fahrtag TuHaKi",
    "startDate" : "2012-06-17",
    "startTime" : "10:00:00"
  },
  "modeltruckerManualEventID-97" : {
    "canceled" : false,
    "clubKey" : "manualClubKey-2",
    "endTime" : "17:00:00",
    "eventOpenForWeightData" : false,
    "link" : "",
    "locationID" : "manualLocationID-4",
    "name" : "Fahrtag TuHaKi",
    "startDate" : "2012-06-17",
    "startTime" : "10:00:00"
  },
  "modeltruckerManualEventID-98" : {
    "canceled" : false,
    "clubKey" : "manualClubKey-2",
    "endTime" : "16:00:00",
    "eventOpenForWeightData" : false,
    "link" : "",
    "locationID" : "manualLocationID-4",
    "name" : "Fahrtag TuHaKi",
    "startDate" : "2012-03-11",
    "startTime" : "10:00:00"
  },
  "modeltruckerManualEventID-99" : {
    "canceled" : false,
    "clubKey" : "manualClubKey-1",
    "endTime" : "16:00:00",
    "eventOpenForWeightData" : false,
    "link" : "",
    "locationID" : "manualLocationID-4",
    "name" : "Fahrtag TuHaKi",
    "startDate" : "2011-11-27",
    "startTime" : "10:00:00"
  }
}

I now want to filter, so I only get events from one "clubKey" which I could achieve using:

.orderByChild("clubKey").equalTo("manualClubKey-1")

That works fine.

But is there any way, that I can order the results also by child "startDate"?

So I want to have the filtered results ordered by startDate finally.

As far as I know this does not work.

I use Firebase on Android with Firebase UI.

Any Ideas how to achieve that?

I do not want to restructure the data or duplicate it further.

Hope there is a way, maybe force the ordering already on storage?

Juergen S.
  • 17
  • 8
  • You will either have to order the data client-side or create a property that combines the values of `clubKey` and `startDate`. For example: `"clubKey_startDate": "manualClubKey-1_2011-11-27"`. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen May 11 '17 at 04:07
  • Thank, but I do not see, how do I get all "manualClubKey-1" with ANY startDate, but ordered by startDate. Or is there a trick using startAt and endAt. On the Link given I did not see a solution to my problem, where it seems to look similar. – Juergen S. May 11 '17 at 19:34
  • Your question seemed to be how to get users with `club == "manualClubKey-1"` order by `startDate`. You'll need to create a composite property for that and then `ref.orderByChild("clubKey_startDate").startAt("manualClubKey-1").endAt("manualClubKey-2")` – Frank van Puffelen May 12 '17 at 04:41
  • I think the confusing part here is, that I tryed to use the same value for startAt and endAt, as the manual states for startAt "=>" and for endAt "<=", but it seems endAt only does "<" lower and not a lower and equal. dbTesting.child('eventList').orderByChild("sortField").startAt("manualClubKey-1").endAt("manualClubKey-2"); works, but dbTesting.child('eventList').orderByChild("sortField").startAt("manualClubKey-1").endAt("manualClubKey-1"); does not. I will look into this aproach, as I only did a quick javascript testing – Juergen S. May 12 '17 at 16:03

1 Answers1

0

If you want to orderByChild(especificdate), is not possible without restructuring the data.

However, lets say you want to get the latest added nodes, just use limitToLast(numberOfChildren)

Martin De Simone
  • 2,108
  • 3
  • 16
  • 30