Firestore querying multiple documents from collection

烈酒焚心 提交于 2020-05-31 05:43:54

问题


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)

For example:

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

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

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


回答1:


What you're asking for is called a "join", and those types of queries are not supported by Firestore. Firestore can only use documents from a single collection at a time in a single query. (The exception is collection group queries which can use documents from multiple collections that all have the same name).

What you will have to do is query for all the documents in "favorites", and use the results of that query to individually get() each related document from "stores".




回答2:


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

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


来源:https://stackoverflow.com/questions/61162167/firestore-querying-multiple-documents-from-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!