Zubair Younas
Zubair Younas

Reputation: 71

Compound query not giving correct results [Firestore]

Here is my table (all values are string type, 'subjects' is an array):

enter image description here

I'm trying:

  FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
  Query q = rootRef.collection("users");


  q = q.whereArrayContains("subjects", "Science").
           whereEqualTo("qualification","0").
               whereEqualTo("gender", "Male").
                  whereLessThanOrEqualTo("fee", "300");

  q.get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {

                        Log.d("LogHeree ", "here " + document.get("name"));

                }
            }
        });

It does not return anything.

I tried:

 q = q.whereArrayContains("subjects", "Science").
               whereEqualTo("qualification","0").
                   whereEqualTo("gender", "Male");

This (above query) gives correct results.

Also:

 q = q.whereLessThanOrEqualTo("fee", "300");

This (above query) also gives correct results. Obviously the query works fine when ran separately. So, what am I doing wrong here or why doesn't it work when combined?

P.S. Not a duplicate of: Firestore compound query with <= & >= As I'm using inequality on a single field. I get this error: "The query needs an index..."

Upvotes: 0

Views: 237

Answers (1)

Ashish
Ashish

Reputation: 6919

I think you wrote wrong collection name :

Change this :

Query q = rootRef.collection("users");

Into this :

Query q = rootRef.collection("Users");

Upvotes: 1

Related Questions