如何在Firestore中使用Android Studio计算集合中的文档数量?

huangapple go评论75阅读模式
英文:

How to Count Number of documents in a collection in Firestore Android Studio?

问题

在Flutter中,我们使用Querysnapshot.documents.length来获取文档数量。

类似地,在Java的Android Studio中我们该如何做呢?

提前致谢。

英文:

In flutter we use the Querysnapshot.documents.length to fetch number of documents?

Similarly , How can we can do it in Java Android Studio

Thanks in advance

答案1

得分: 2

代替循环,只获取大小

db.collection("Store")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
               Log.d(TAG, "大小为", task.getResult().size());
            } else {
                Log.d(TAG, "获取文档时出错:", task.getException());
            }
        }
    });
英文:

Instead of looping just get size

db.collection(&quot;Store&quot;)
    .get()
    .addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
        @Override
        public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
            if (task.isSuccessful()) {
               Log.d(TAG, &quot;Size is&quot;, task.getResult().size());
            } else {
                Log.d(TAG, &quot;Error getting documents: &quot;, task.getException());
            }
        }
    });

答案2

得分: 1

已经在这里回答过了。没有内置的方法,实际上,我们必须使用for循环来计算文档数量。

英文:

Already Answered Here

No built in Method Actually, We have to count the documents using for loop

huangapple
  • 本文由 发表于 2020年9月30日 12:37:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64130967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定