Adam Ma
Adam Ma

Reputation: 688

Firestore querying multiple documents from collection

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

Upvotes: 1

Views: 1066

Answers (2)

Adam Ma
Adam Ma

Reputation: 688

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

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317467

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".

Upvotes: 1

Related Questions