Sanady_
Sanady_

Reputation: 80

Why all where filters with an inequality is returning exception from Firestore?

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?

Upvotes: 1

Views: 1415

Answers (1)

ianhanniballake
ianhanniballake

Reputation: 200120

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.

Upvotes: 2

Related Questions