android - 如何从 Cloud Firestore android 中的父集合中获取集合和子集合
问题描述
我有 user_info 作为父集合。在这个父集合下,它有 single_list 作为子集合和一些信息。我想从父集合中获取所有值。请帮我找到答案。提前致谢
解决方案
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users_info")
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
//Here you can add all documents Id to your documentIdList to fetch all sigle_data at once.
Log.d(TAG, document.getId() + " => " + document.getData());
documentIdList.add(document.getId());
}
getAllSubCollSingleData(documentIdList);
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
public void getAllSubCollSingleData(List<Int> documentIdList){
for(int i=0;i<documentIdList.size();i++){
db.collection("users_info").document(documentIdList.get(i))(would be phone number)
.collection("single_data")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
//Here you can get all documents in sub collection single_data
}
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
}}
推荐阅读
- c# - 获取响应规则的日期列表
- asynchronous - 事件循环何时在 Dart 中启动以及事件队列如何工作
- java - java在使用mockMvc测试spring boot应用程序时无法解析符号'param'
- .net - 在 F# 中分别设置 DateTime 的日期/时间
- node.js - 如何在 Electron 的 MacOS 菜单栏右侧添加图标?
- r - 如何在 R 中使用聚集而不是联合
- firebase - 如何使用云功能保护 Firebase 消息传递主题?
- matlab - 如何在热图中设置 x 轴值
- sql - 皱眉(错误)错误消息:“值不在预期范围内。” - 在 ListObjects.Add().QueryTable
- java - FragmentManager 无法在 androidx 中解析