I need help with Firestore queries. I have a all_users
data collection, user id-documents with each user information. firestore database image i want to check if username already exists. i get how to get() documents and compare as demonstrated on their webpage but what about data query?, this is my code
updating the widgets - (if mUser text field and current username is not same)
private void saveProfileSettings(){
final String username = mUsername.getText().toString();
//Case 1: user did not change their username
if (!mUsers.getUsername().equals(username)){
checkingIfusernameExist(username);
}else {
}
}
checkingIfusernameExist method
private void checkingIfusernameExist(final String username){
Log.d(TAG, "checkingIfusernameExist: Checking if " + username + " Exists");
Query mQuery = mFirebaseFirestore.collection("all_users")
.orderBy(getString(R.string.fields_username))
.whereEqualTo("username", username);
mQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (documentSnapshots != null){
Log.d(TAG, "onEvent: username does not exists");
Toast.makeText(getActivity(), "Username is available", Toast.LENGTH_SHORT).show();
}
for (DocumentSnapshot ds: documentSnapshots){
if (ds.exists()){
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH: " + ds.toObject(Users.class).getUsername());
Toast.makeText(getActivity(), "That username already exists.", Toast.LENGTH_SHORT).show();
}
}
}
});
}
I don't get any errors neither do results. I have searched everywhere and I haven't seen issues as mine. Plus there aren't much queries around i can work with. any correction would be appreciated, thanks in advance.
UPDATE : after days of searching i actually came up with a solution with the help of answers i got below. so, since firestore does not not have an operational logic, and you want to update if username does not exists with the .whereEqualTo, use the task to find contains any payload.
code that worked for me
checkingIfUsernameExists method
private void checkingIfusernameExist(final String usernameToCompare){
//----------------------------------------------------------------
final Query mQuery = mFirebaseFirestore.collection("all_users").whereEqualTo("username", usernameToCompare);
mQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Log.d(TAG, "checkingIfusernameExist: checking if username exists");
if (task.isSuccessful()){
for (DocumentSnapshot ds: task.getResult()){
String userNames = ds.getString("username");
if (userNames.equals(usernameToCompare)) {
Log.d(TAG, "checkingIfusernameExist: FOUND A MATCH -username already exists");
Toast.makeText(getActivity(), "username already exists", Toast.LENGTH_SHORT).show();
}
}
}
//checking if task contains any payload. if no, then update
if (task.getResult().size() == 0){
try{
Log.d(TAG, "onComplete: MATCH NOT FOUND - username is available");
Toast.makeText(getActivity(), "username changed", Toast.LENGTH_SHORT).show();
//Updating new username............
}catch (NullPointerException e){
Log.e(TAG, "NullPointerException: " + e.getMessage() );
}
}
}
});