Firebase Firestore嵌套查询检索两个结果

时间:2018-09-20 18:00:37

标签: android firebase google-cloud-firestore

所以我正在尝试从Firestore获取结果

db.collection("dialogs")
            .whereArrayContains("members", me.getId())
            .get()
            .continueWith(continue -> {
                List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
                for (DocumentSnapshot snapshot : continue.getResult())
                    for (Object userId : (ArrayList) snapshot.get("members"))
                        tasks.add(db.collection("users").document(userId.toString()).get());
                return tasks;
            })
            .addOnSuccessListener(task -> {
                for (Object something : task)
                    Log.d("Query", "Data: " + something);
            })
            .addOnFailureListener(e -> Log.d("Query", "Exception: " + e));

此代码将为我提供如上所示的users个文档,我也想要的是snapshot的文档ID

1 个答案:

答案 0 :(得分:0)

请注意,continue不是文档,它是包含DocumentSnapshot结果的Task。您的DocumentSnapshot位于snapshot中,您可以使用其getId()方法获取DocumentSnapshot的ID。

val id = snapshot.getId()