title: How to check a certain data already exists in firestore or not
body: <p>Before adding a new data into the firestore, i want to check already a data of the same kind exists in the database or not.if already a data was present means i want to prevent the user from entering duplicate data in the database.
  In my case it is like a appointment booking if already a booking for the same time exists,i want to prevent to users to book on the same time.i tried using query function but it is not preventing duplicate data entering.someone plz help me</p>
  
  <pre><code>private boolean alreadyBooked(final String boname, final String bodept, final String botime) {
          final int[] flag = {0};
          CollectionReference cref=db.collection("bookingdetails");
          Query q1=cref.whereEqualTo("time",botime).whereEqualTo("dept",bodept);
          q1.get().addOnSuccessListener(new OnSuccessListener&lt;QuerySnapshot&gt;() {
              @Override
              public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                  for (DocumentSnapshot ds : queryDocumentSnapshots) {
                      String rname, rdept, rtime;
                      rname = ds.getString("name");
                      rdept = ds.getString("dept");
                      rtime = ds.getString("time");
                      if (rdept.equals(botime)) {
                          if (rtime.equals(botime)) {
                              flag[0] = 1;
                              return;
                          }
                      }
                  }
              }
          });
          if(flag[0]==1){
              return true;
          }
          else {
              return false;
          }
      }
  </code></pre>