【问题标题】:How to collect all firestore filed value如何收集所有 Firestore 字段值
【发布时间】:2018-01-08 07:06:11
【问题描述】:

我正在使用 firestore,我正在尝试收集所有字段值,但我找不到简单的方法,所以我要收集的是总用户费率..

所以这是我正在寻找的示例:

我需要收集“rate_value”字段:

Rates > Key_A > rate_value: 2
Rates > Key_B > rate_value: 5
Rates > Key_C > rate_value: 1
Rates > Key_D > rate_value: 4

enter image description here

那么我怎样才能得到总费率 (13)..

我使用了下面的代码,但它返回了我

mPostsCollection.document(Post_Key).collection("Rates").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override public void onEvent(QuerySnapshot querySnapshot, FirebaseFirestoreException e) {

            if (e==null){

                Long Rates = (long) querySnapshot.getDocuments().size();

                for (DocumentSnapshot mDocs : querySnapshot.getDocuments()){

                    Long Rate_Value = mDocs.getLong("rate_value");

                    mPostValue.setText((int) (Rate_Value/Rates));

                }
            }
        }}
    );

【问题讨论】:

  • 任何人都可以帮助我,我所需要的只是获得“价值”字段的总数..

标签: android firebase google-cloud-firestore


【解决方案1】:

引用Firestore documentation

Cloud Firestore 中的高级查询可让您快速查找大型集合中的文档。如果您想深入了解整个集合的属性,则需要对集合进行聚合。

因此,要获得所有值的总和,您需要 get the documents 并添加每个文档的值。

【讨论】:

  • 嘿@FrankvanPuffelen 检查问题的新变化。
猜你喜欢
  • 1970-01-01
  • 2021-04-22
  • 2019-12-29
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 2021-12-12
  • 2016-02-27
  • 1970-01-01
相关资源
最近更新 更多