Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude an element from a Firestore query?

I have a collection of users and I want to query all users from the database and display them in a RecyclerView except one, mine. This is my db schema:

users [collection]
  - uid [document]
     - uid: "fR5bih7SysccRu2Gu9990TeSSyg2"
     - username: "John"
     - age: 22
  - //other users

How to query the database like so:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query q = db.collection("users").whereNotEqualTo("uid", uid);

So I need this query object to be passed to a FirestoreRecyclerOptions object in order to display all the other users in RecyclerView.

Is this even possible? If not, how can I solve this? Thanks!

Edit:

options = new FirestoreRecyclerOptions.Builder<UserModel>()
        .setQuery(query, new SnapshotParser<UserModel>() {
            @NonNull
            @Override
            public UserModel parseSnapshot(@NonNull DocumentSnapshot snapshot) {
                UserModel userModel = documentSnapshot.toObject(UserModel.class);
                if (!userModel.getUid().equals(uid)) {
                    return userModel;
                } else {
                    return new UserModel();
                }
            }
        }).build();
like image 922
Joan P. Avatar asked Aug 15 '18 13:08

Joan P.


1 Answers

2021 Update: This Is Supported

Howdy devs. It looks like this is now supported with the where operator used like this: citiesRef.where("capital", "!=", false);

like image 92
Davis Jones Avatar answered Oct 11 '22 23:10

Davis Jones