【问题标题】:How to get count of documents in a collection in firestore [duplicate]如何获取firestore中集合中的文档计数[重复]
【发布时间】:2018-05-25 21:04:29
【问题描述】:

我有一个收藏“商店”

因为有很多文档

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

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

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

      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());
                }
            }
        });
    

    源:get data with Firestore

    【讨论】:

    • 我在 ApiFuture 和 future.get() 中遇到错误
    • @Ramakrishna 编辑,意外地选择了 java 代码进行查询而不是 android,这应该可以工作,db 是你的数据库实例,count 是你的文档计数,或者如果你没有得到 smth 检查链接文档
    • 不需要运行循环使用这个task.getResult().size()
    • 你能帮我解决这个问题吗stackoverflow.com/questions/54653836/…
    • @VikashSharma 如果我确定你的意思,请将你的建议作为一个独立的答案发布,这样如果你觉得它更优化,它可以帮助人们
    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2020-07-29
    • 2018-04-09
    • 1970-01-01
    • 2018-04-09
    • 2018-07-10
    • 1970-01-01
    相关资源
    最近更新 更多