Firestore query - how to find by value with unknow key of the parent?

66 Views Asked by At

Trying to make a query. But need to find the document if i dont know the parent key of it. So in my db i want to find user by name when i dont know uid. My db below enter image description here

Request code

      users.whereEqualTo("name", "DavidMells")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        //Here size is 0 nothing found
                        int size = task.getResult().size();
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            FsUser fsUser = document.toObject(FsUser.class);
                            String testingValue = fsUser.getCountry();
                            Log.d("log", document.getId() + " => " + document.getData().get("country"));
                        }
                    } else {
                        Log.d("log", "Error getting documents: ", task.getException());
                    }
                }
            });

The declaration of reference

private CollectionReference users = FirebaseFirestore.getInstance().collection(Consts.USERS_DB);

The constant is

    public static final String USERS_DB = "Users";
1

There are 1 best solutions below

1
vitooh On BEST ANSWER

I dont know... Maybe this too simple... but I don't see other option, as the code seems to be proper:

Looking on the screenshot I noticed that there is space after DavidMells in the database, but not in code...

enter image description here