如何获取firestore中集合中的文档计数

Ram*_*hna 2 android firebase google-cloud-firestore

我有一个收藏“商店”

拥有大量文件

我如何获取该集合中的文档数量。

Rai*_*ker 7

我相信没有内置的方法,但你可以尝试这个!首先获取列表中的所有文档,然后获取大小。

  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());
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

Src:使用 Firestore 获取数据

  • 不需要运行循环使用这个task.getResult().size() (9认同)