0

I apologize for having to ask this question, I have been researching countless links but have not been able to find it. It seems simple but has been far from it.

I know that you can not do a not equal to query in firestore, thus my code is the following:

private void getNotEqual() {
    // db defined elsewhere
    Query query = db.collection("collection")
            .whereLessThan("uid", user.getUid())
            .whereGreaterThan("uid", user.getUid())
            .orderBy("uid");

    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            Log.d(TAG, "Size: " + task.getResult().size());
        }
    });
}

But it is still not returning the documents where it is not equal. The Uid is from Firebase Authentication and is stored as a string as a field in the document. Am I missing something here?

Thank you sincerely in advance.

enter image description here

dmo
  • 63
  • 8

2 Answers2

2

This query is always going to return no documents. It is effectively asking for all documents whose uid field is both greater than and less than a certain value, which is an impossible condition to satisfy. The filters in a Firestore query are logically AND'd together. They are not logically OR'd. Firestore does not support logical ORs in queries.

It sounds like you want to perform two separate queries, then combine the results in the client. One query will have whereLessThan("uid", user.getUid()) and the other whereGreaterThan("uid", user.getUid()). But honestly, it might be easier for you just to get all the documents in the collection, and manually filter out the ones you don't want.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Firestore v21.7.0 introduces not equal queries, so you can now use:

private void getNotEqual() {
    // db defined elsewhere
    Query query = db.collection("collection")
            .whereNotEqualTo("uid", user.getUid())
            .orderBy("uid");

    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            Log.d(TAG, "Size: " + task.getResult().size());
        }
    });
}