1

long story short, I am making an application for train timestamps, and so I have a cloud firestore database as my DB. The point of issue is that when users find specific times for departure I am using whereGreaterThanOrEqualTo for searching time, to be specific minutes and hours. If a user tries to find a route for 8:30, the application should return to the next route which is around that time. But when the user click finds a route, the app crashes... Here is the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projekatidemovozom/com.example.projekatidemovozom.SecondActivity}: java.lang.IllegalArgumentException: All where filters with an inequality (notEqualTo, notIn, lessThan, lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the same field. But you have filters on 'departure.hours' and 'departure.minutes'

This is my code for fetching data from the database:

public void initDatabaseConnection() {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        CollectionReference timestampsRef = db.collection("timestamps");
        timestampsRef.whereEqualTo("route.cityFrom", cityOd)
                .whereEqualTo("route.cityTo", cityDo)
                .whereGreaterThanOrEqualTo("departure.hours", hours) //ISSUE LINE
                .whereGreaterThanOrEqualTo("departure.minutes", minutes) //ISSUE LINE
                .get()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            TimestampModel tm = document.toObject(TimestampModel.class);
                            timestampModelList.add(tm);
                            Log.d(TAG, tm.toString());
                        }
                    } else {
                        Log.w(TAG, "Error getting documents.", task.getException());
                    }
                });
    }

This is my database structure:

data

So do you know what is the problem?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sanady_
  • 68
  • 10
  • 4
    The exception seems pretty clear: you can't have two `whereGreaterThanOrEqualTo` statements on different fields. TBH, I don't think your query works as you expect - 9:00 won't match since 0 is less than 30 minutes. Why is hours and minutes separate fields? – ianhanniballake Feb 02 '21 at 21:01
  • 3
    That last part is actually a good solution @ianhanniballake, as it allows the use-case with a different (single-field) query. Can you write up up as an answer? Otherwise chances are this will be closed as a duplicate of another question on the same error message. – Frank van Puffelen Feb 02 '21 at 21:07
  • Ahh okay, I haven't noticed that there can't be two `whereGreaterThanOrEqualTo` statements. Thanks, I gave you thumbs-up for this answer :) @ianhanniballake I would like to set it up as an answer but I can't... :( @FrankvanPuffelen – Sanady_ Feb 02 '21 at 22:01

1 Answers1

2

The exception states:

All where filters with an inequality (notEqualTo, notIn, lessThan, lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the same field. But you have filters on 'departure.hours' and 'departure.minutes'

So you can't have two whereGreaterThanOrEqualTo statements on different fields (your hour and minute fields).

In fact, having the time represented as two fields means your query doesn't work as you expect since a time of 9:00 wouldn't match if you query for 8:30 since 0 is less than 30.

Instead, consider using a single field to represent the time (say, minutes since midnight) which would mean you could use just a single whereGreaterThanOrEqualTo.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thank you for your answer. So I should use time as a single field string? And then I could apply `whereGreaterThanOrEqualTo` method right? – Sanady_ Feb 04 '21 at 09:32
  • "minutes since midnight", my recommendation, would be an integer value. You really want to be comparing integer values for time, not strings. – ianhanniballake Feb 04 '21 at 15:45
  • Hmm okay, I haven't think about that.. Thanks for your answer :) – Sanady_ Feb 04 '21 at 16:54