[英]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 系列:
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.