簡體   English   中英

使用文檔 ID 從 Firestore 獲取數據

[英]Get data from firestore using document id

我的firestore數據庫中有兩個collections,第一個是所有文檔的列表(BlockList),第二個是用戶的。 當用戶在應用程序上為帖子添加書簽時,僅將此帖子的 id 發送到子集合(收藏夾)。

那么我如何根據它的 id 顯示來自第一個集合的這個子集合的文檔。

圖 1

圖 2

firebaseFirestore.collection("Users")
                .document(userId).collection("Favorites").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    List<String> list = new ArrayList<>();
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        list.add(document.getId());
                    }
                    Log.d(TAG, list.toString());
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

我使用此代碼到達子集合 ID 的列表,但我想知道如何使用它從主集合(BlockList)中獲取適合此 ID 的文檔。

在循環之后,您已經有了一個 id 列表,只需遍歷它們並在blockedList 中找到它們:

....
....
for (QueryDocumentSnapshot document : task.getResult()) {
list.add(document.getId());
}

//here loop through the list

for(int i = 0 ; i<list.size() ; i++){

//now refer to the id in the blocked list
firebaseFirestore.collection("BlockList").document(list.get(i)).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {

    .........

    });



}

......
......
    userRef.document(reference)
                    .collection(favCollect)
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if (task.isSuccessful()) {
                                task.getResult()
                                        .getQuery()
                                        .addSnapshotListener((queryDocumentSnapshots, e) -> {
List<DocumentChange> documentChanges = queryDocumentSnapshots.getDocumentChanges();
                                    for (int i = 0; i < documentChanges.size(); i++) {

}

}

這樣你就可以找到你要找的id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM