Rahul Shukla
Rahul Shukla

Reputation: 1292

Cloud Firestore query result set is empty

I am trying to run the below query on the sample data provided in firebase docs here:

cities.whereLessThan("population", 9000000)
        .whereGreaterThan("population", 9000000)
        .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if(task.isSuccessful()) {
            Logger.log_error(TAG + " Task successful Result is Empty = " + task.getResult().isEmpty());
        }else {
            Logger.log_error(TAG + " Task failed = " + task.getException().getMessage());
        }
    }
});

I am expecting to get cities where the population is less than 9000000 and greater than 9000000. Eventually what I am trying to achieve is: select * from TABLE where population <> 9000000 by combining whereLessThan and whereGreaterThan in the FireStore query.

Can someone please let me know if it isn't supported at all or am I wrong somewhere?

Upvotes: 0

Views: 1680

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598708

Firestore query conditions are ANDed together. So what you're asking for is: give me a cities with more than 9 million and with less than 9 million people. That's an empty set: no city can have both more and less than 9 million.

Upvotes: 1

Erik
Erik

Reputation: 5119

If you haven't already I believe you have to create a Cloud Firestore index

in short:

Cloud Firestore requires an index for every query, to ensure the best performance. All document fields are automatically indexed, so queries that only use equality clauses don't need additional indexes. If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.

Upvotes: 0

Related Questions