1

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

Adam Ma
  • 624
  • 5
  • 16
  • To retrieve up to 10 documents by their ID, you can [use an `in` query](https://firebase.google.com/docs/firestore/query-data/queries#in_and_array-contains-any). See https://stackoverflow.com/questions/46721517/google-firestore-how-to-get-document-by-multiple-ids-in-one-round-trip – Frank van Puffelen Apr 11 '20 at 19:16

2 Answers2

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

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    I managed to get the data as needed using 'in' query but still lost RecyclerView real-time updating functionality where any update query isn't mirrored in real-time and I need to refresh – Adam Ma Apr 11 '20 at 20:09
  • It sounds like you now have a different problem than the one you started out with. I suggest asking a new question with the code that isn't working the way you expect. – Doug Stevenson Apr 11 '20 at 20:12
1

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());
                        }
                    }
                });
Adam Ma
  • 624
  • 5
  • 16