0

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..."

Fernando C.
  • 201
  • 1
  • 11
Zubair Younas
  • 71
  • 1
  • 13
  • Your code is not doing anything in response to errors that might happen on the query (isTaskSuccessful could return false). What do you find if you check for errors as shown in the documentation? https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection – Doug Stevenson Mar 09 '20 at 07:29
  • Also bear in mind that you are storing several numbers as strings, and that's very likely to cause you problems with range queries and comparisons, since strings don't sort like numbers. You should never store numbers as strings unless you absolutely know what you're doing. – Doug Stevenson Mar 09 '20 at 07:30
  • Thanks for reviewing, I tried chaning the data types to number but still same result. I'll try getting the error and add. – Zubair Younas Mar 09 '20 at 07:35
  • So what exactly is the error? Please edit the question to be **specific** and **complete** about what you observe. – Doug Stevenson Mar 09 '20 at 15:18
  • Which error are you exactly getting? Maybe you just don't have fields in your table satisfying all the conditions. – Fernando C. Mar 09 '20 at 15:59

1 Answers1

1

I think you wrote wrong collection name :

Change this :

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

Into this :

Query q = rootRef.collection("Users");
Ashish
  • 6,791
  • 3
  • 26
  • 48