Firestore从集合中查询多个文档 [英] Firestore querying multiple documents from collection

查看:43
本文介绍了Firestore从集合中查询多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firestore中,我有一个用户集合,并且在每个用户文档中存储了一个名为收藏夹"的集合,其中包含标记为收藏夹(存储)"的文档的ID.

In Firestore I have a users collection and within each user document is stored a collection named favorites which contains the IDs of documents marked as favorites(stores)

例如:

in users/2pfV9FbtwPYFmQHz3KU2BKmhMr82/favorites 
I have multiple documents such as 7F9COWGW3Ww8FWiH8VTA and 8b8WogHzpqCkw0ZxMjOw

我想查询一个查询,该查询从名为stores的集合中返回所有具有相同docID的文档其中包含这2个ID以及更多ID(收藏夹"列表中没有)

I would like to make a query that returns all of the documents with the same docID from a collection called stores which contains these 2 IDs and many more(that are no in favorites list)

A similar query will be
SELECT * FROM stores WHERE docID EXISTS IN favorites

我可以采用另一种方法来获取两个集合并手动对其进行过滤,但是我使用的是Firebase RecyclerView适配器,该适配器显示的所有数据均基于查询,这将使事情更加高效.

I could take another approach to get both collections and manually filtrate them, but I am using Firebase RecyclerView adapter which all data displayed is based on the Query and will make things more efficient.

如何获得这种结果?让我知道是否需要进一步的解释

How can such result be achieved? let me know if further explanation is needed

推荐答案

我上述问题的可能答案是从收藏夹"集合中获取所有文档ID,并将它们放入列表中然后使用whereIn函数将商店集合ID与收藏夹列表ID结合起来

A possible answer for my above question is to get all the documents IDs from Favorites collection and put them into a list and then use whereIn function to join the stores collection IDs with the favorites list IDs

主要问题是使用Firebase RecyclerView时完全失去实时更新功能

The major issue, is a complete loss of real-time updating functionality when using Firebase RecyclerView

favoriteList = new ArrayList<>();
        favCollection.get() //get user favorites
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                favoriteList.add(0, document.getId());
                                Log.d("Favorites", document.getId() + " => " + document.getData());
                            }
                            query = storesCollection.whereIn(FieldPath.documentId(), favoriteList).orderBy("storeName");//FieldPath.documentId gives documents IDs in stores collection which will be filtered with the provided list
                            attachRecyclerViewAdapter();//Initiate adapter after favorites list completion
                        } else {
                            Log.d("Favorites", "Error getting documents: ", task.getException());
                        }
                    }
                });

这篇关于Firestore从集合中查询多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆