How to get count of documents in a collection in f

2019-09-21 00:27发布

This question already has an answer here:

i have a collection "Store"

in that having number of documents

how i get the count of documents in that collection.

1条回答
地球回转人心会变
2楼-- · 2019-09-21 00:56

I believe there is no built in method for this but you can try this! First get all the documents in a List and then just get the size.

  db.collection("Store")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
               int count = 0;
                for (DocumentSnapshot document : task.getResult()) {
                   count++;
                }
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

Src:get data with Firestore

查看更多
登录 后发表回答