How to check if Cloud Firestore collection exists? ( querysnapshot)

exists() applies to DocumentSnapshot while you're dealing with QuerySnapshot

Call task.result for getting QuerySnapshot out of Task<QuerySnapshot>.

From that, call result.getDocuments() and iterate through each of the DocumentSnapshot calling exists() on them.


Use DocumentSnapshot.size() > 0 to check if the collection exists or not.

Here is an example from my code:

         .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                if(task.getResult().size() > 0) {
                    for (DocumentSnapshot document : task.getResult()) {
                        Log.d(FTAG, "Room already exists, start the chat");

                } else {
                    Log.d(FTAG, "room doesn't exist create a new room");

            } else {
                Log.d(FTAG, "Error getting documents: ", task.getException());