从 firestore 中的文档集合和文档子集合查询获取数据

get data with query from document collection and document subcollection in firestore

我有一个名为“grup”的集合和一个名为“anggota”的子集合。

请帮我,如何通过查询引用集合文档和子集合文档来显示数据。

Firestore 集合:

我试过这个查询,但它不起作用,数据没有出现

db.collection("grup").document().collection("anggota")
            .whereEqualTo("iduser", idUser)
            .orderBy("updatetime", Query.Direction.DESCENDING)
        .addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                List<DocumentSnapshot> list = value.getDocuments();

                datalist.clear();
                for (DocumentSnapshot d : list) {
                    final Modelfirestore c = d.toObject(Modelfirestore.class);
                    datalist.add(c);
                    
                }
                mAdapterss.notifyDataSetChanged();
            }
        });

当您使用以下查询时:

                              
db.collection("grup").document().collection("anggota")
        .whereEqualTo("iduser", idUser)
        .orderBy("updatetime", Query.Direction.DESCENDING)

这意味着您正在创建对具有随机 ID 的文档的引用。调用 CollectionReferenc#document() 方法,不传递任何参数:

Returns a DocumentReference pointing to a new document with an auto-generated ID within this collection.

因此,为了能够查询 anggota 子集合中的文档,您必须将文档 ID 传递给 document() 方法,如下所示:

                                 
db.collection("grup").document("8FDD...").collection("anggota")
        .whereEqualTo("iduser", idUser)
        .orderBy("updatetime", Query.Direction.DESCENDING)