title: Firestore query - checking if username already exists
body: <p>I need help with Firestore queries. I have a <code>all_users</code> data collection, user id-documents with each user information. <a href="https://i.stack.imgur.com/Wq7vg.jpg" rel="noreferrer">firestore database image</a> 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</p>
  
  <p>updating the widgets - (if mUser text field and current username is not same)</p>
  
  <pre><code> 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 {
  
      }
  }
  </code></pre>
  
  <p>checkingIfusernameExist method</p>
  
  <pre><code>    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&lt;QuerySnapshot&gt;() {
          @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();
                  }
              }
          }
      });
   }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>code that worked for me</p>
  
  <p>checkingIfUsernameExists method</p>
  
  <pre><code>private void checkingIfusernameExist(final String usernameToCompare){
  
      //----------------------------------------------------------------
      final Query mQuery = mFirebaseFirestore.collection("all_users").whereEqualTo("username", usernameToCompare);
      mQuery.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
          @Override
          public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; 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() );
                  }
              }
          }
      });
  </code></pre>