遍历子集合以获取字段-Firestore Android [英] Iterate through sub collections to get fields - Firestore Android
问题描述
每个 Notification ID
集合仅包含一个文档,我想对 ToseefShop1
中的所有集合进行迭代,并获取各自的文档名称和字段数据.
Each Notification ID
collection contain only one document and I want to Iterate all the collections in ToseefShop1
and get the respective document name and fields data.
数据模型:
子集合:
代码:
dbRef.collection("Shop Notifications")
.document("ToseefShop1")
.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot querySnapshot) {
// Dont know what to do
}
});
这不是重复的问题.另一个问题(有人建议与之重复)是关于javascript的,而答案是关于Node.js的.没有答案被接受.实际上,我在Firestore Java中找不到getcollections()方法.
It's not a duplicate question. The other question (someone suggested as duplicate to) is about javascript and answers are for Node.js. With no answer accepted. Infact I can not find getcollections() method in firestore Java.
推荐答案
Firestore中无法查询文档以获取其下的子集合.为了获得所需的文档名称和字段数据,首先需要具有子集合的名称,然后在引用中使用它们.如果您只有2个子集合,建议您使用以下代码:
There is no way in Firestore to query a document to get the subcollections beneath it. In order to get the document name and fields data that you are asking for, first you need to have the names of subcollections and then use them in a reference. If you have only 2 subcollections, I suggest you to use the following code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query firstQuery = rootRef
.collection("Shop Notifications")
.document("ToseefShop1")
.collection("Notification ID:0");
Query secondQuery = rootRef
.collection("Shop Notifications")
.document("ToseefShop1")
.collection("Notification ID:1");
Task firstTask = firstQuery.get();
Task secondTask = secondQuery.get();
Task combinedTask = Tasks.whenAllSuccess(firstTask, secondTask).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
@Override
public void onSuccess(List<Object> list) {
//Do what you need to do with your list
}
});
这篇关于遍历子集合以获取字段-Firestore Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!