tccpg288
tccpg288

Reputation: 3352

Cloud Firestore - Cannot Get Document Count

I am migrating from Firebase realtime database and previously used the .getChildrenCount method to get the count of items in a specific node.

This method does not appear to have a counterpart in the Cloud Firestore. I am trying to access the answers collection in my database and get the count. Below is my code, everything is reading correctly and I am noting where I begin attempting to get the count of documents in the answers collection:

mStoreSelectedPollRef.collection(ANSWERS_LABEL).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    int numberOfPollAnswers = 0;
                    int pollAnswerSize = task.getResult().size();
                    for (DocumentSnapshot answer : task.getResult()){
                        numberOfPollAnswers++;
                    }
                    Log.v("NUMBER OF POLL ANSWERS ", String.valueOf(numberOfPollAnswers));
                    Log.v("SIZE", String.valueOf(pollAnswerSize));

// addRadioButtonsWithFirebaseAnswers(numberOfPollAnswers, task.getResult()); } });

Below is my data structure:

enter image description here

Upvotes: 0

Views: 587

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598708

Your mStoreSelectedPollRef seems to be a DocumentReference, which points to a single document. The size() method only exists on QuerySnapshot, which you get when you attach a listener to a Query.

Upvotes: 1

Related Questions