使用文档 ID 从 Firestore 获取数据

huangapple go评论94阅读模式
英文:

Get data from firestore using document id

问题

我在我的Firestore数据库中有两个集合,第一个是所有文档的列表(BlockList),第二个是用户集合。当用户在应用程序中收藏帖子时,仅发送此帖子的ID到子集合(Favourites)。

那么如何基于这些ID从第一个集合(BlockList)中显示这个子集合的文档呢?

我使用以下代码来获取子集合ID的列表,但我想知道如何使用它从主集合(BlockList)中获取适用于这些ID的文档。

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());
                }
            }
        });
英文:

I have two collections in my firestore database, the first is list of all the documents (BlockList), and the second for the users. when the user bookmark post on the app, send only the id of this post to sub-collection (Favourites).

so how can i display this sub-collection's documents from the first collection based on its ids.

使用文档 ID 从 Firestore 获取数据

使用文档 ID 从 Firestore 获取数据

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

I Use this code to reach the list of the sub-collection ids, but i want to know how to use it to get the documents that appropriate to this ids from the main collection(BlockList).

答案1

得分: 1

循环结束后,您已经拥有一个 ID 列表,只需遍历该列表并在 blockedList 中查找相应的 ID:

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

// 在这里遍历列表
for (int i = 0; i < list.size(); i++) {

    // 现在引用被阻止列表中的 ID
    firebaseFirestore.collection("BlockList").document(list.get(i)).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
       
        .........

        });
    }
}

....
....
英文:

After the loop you already have a list of the ids, just loop through them and find them in blockedList:

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

//here loop through the list

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

//now refer to the id in the blocked list
firebaseFirestore.collection(&quot;BlockList&quot;).document(list.get(i)).get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
   
    .........

    });



}

......
......

答案2

得分: 0

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++) {
    
    }
    
    }

In this way you can reach the id you are looking for
英文:
    userRef.document(reference)
                    .collection(favCollect)
                    .get()
                    .addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
                        @Override
                        public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                            if (task.isSuccessful()) {
                                task.getResult()
                                        .getQuery()
                                        .addSnapshotListener((queryDocumentSnapshots, e) -&gt; {
List&lt;DocumentChange&gt; documentChanges = queryDocumentSnapshots.getDocumentChanges();
                                    for (int i = 0; i &lt; documentChanges.size(); i++) {

}

}

In this way you can reach the id you are looking for

huangapple
  • 本文由 发表于 2020年5月30日 03:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/62093136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定