简体   繁体   English

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

[英]get data with query from document collection and document subcollection in firestore

I have a collection with "grup" name and have a subcollection named "anggota".我有一个名为“grup”的集合,还有一个名为“anggota”的子集合。

Please help me, how to display data by query referring to collection document and subcollection document.请帮助我,如何通过查询参考集合文档和子集合文档来显示数据。

Firestore collection: Firestore 系列:

我的 Firestore 中的收藏品

I've tried with this query but it doesn't work, the data doesn't appear我试过这个查询但它不起作用,数据没有出现

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

When you are using the following query:当您使用以下查询时:

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

It means that you are creating a reference to a document with a random ID.这意味着您正在创建对具有随机 ID 的文档的引用。 Calling CollectionReferenc#document() method, without passing any arguments:调用CollectionReferenc#document()方法,不传递任何 arguments:

Returns a DocumentReference pointing to a new document with an auto-generated ID within this collection.返回指向新文档的 DocumentReference,该文档在此集合中具有自动生成的 ID。

So to be able to query the documents within the anggota subcollection you have to pass the document ID to the document() method like this:因此,为了能够查询anggota子集合中的文档,您必须将文档 ID 传递给document()方法,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有来自父文档的子集合引用的 Firestore 查询 - Firestore query with subcollection reference from parent document 从 flutter firestore 中的子集合中获取特定文档 - fetching a specific document from a subcollection in flutter firestore 是否可以在不在 Firestore 中添加文档的情况下将子集合添加到集合中? - Is it possible to add a subcollection to a collection without adding document in Firestore? 从 2 collection firestore 获取所有文档 - get all document from 2 collection firestore 我如何根据其子集合 firestore 中的内容获取文档? - how do i get a document based on whats in its subcollection firestore? 如何在 Firestore 集合组查询 Flutter 中获取子集合文档 - How to Get Subcollection docs in Firestore Collection Group Query Flutter 从 Firestore 子集合 flutter 获取所有数据 - get all data from a firestore subcollection flutter 使用自定义子集合 ID 在 firestore 数据库中设置文档 - Setting a document in firestore database with a custom subcollection id 无法从集合 firebase firestore 中获取所有文档 - Can't get all document from collection firebase firestore 在 geofirestore 查询产生的每个文档下添加一个子集合 - add a subcollection under each document that resulted from geofirestore query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM