Search code examples
firebasegoogle-cloud-firestoreionic4angularfire

How to make an inner join in the Firestore database with the AngularFireBase library?


I currently have 2 collections, one is called "cart" and contains a list of product IDs and the second is called "products", what I want to achieve is to obtain a single list that includes all the data and products that the user already added to Your shopping cart

I have a couple of days looking for some way to make the "internal union" but I have not had much success and the information I find is already more than 1 year, I leave some links of the information i read

https://angularfirebase.com/lessons/firestore-joins-similar-to-sql/ https://angularfirebase.com/snippets/combine-firestore-collections-into-a-single-array-observable/

For now I have the cart list, which contains the products that the user agreed.

cart
  KeyFirestore1
    [0]
      id_product:product1
    [1]
      id_product:product2

And the list of products

products
  product1
    name:"apple"
    price:20.00
    id_product:"product1"
  product2
    nombre:"ice cream"
    price:40.00
    id_product:"product2"

and I would like to only get only one list when consulting

cart_products
  KeyFirestore1
    [0]
      name:"apple"
      price:20.00
      id_product:"product1"
    [1]
      name:"ice cream"
      price:40.00
      id_product:"product2"

Solution

  • In order to do that, you shall use forkJoin. This answer explains how to implement it.

    As mentioned in the answer, "If you need to react on updates from any of the streams — try combineLatest."

    Here are more details about combineLatest operator and a full example (please read the second scenario).