'Firestore query based on parent document fields

Suppose I have the following structure in Firestore: enter image description here

Is there a way I can query (using the JS API) for a list of reviews from orders whose shopId is a specific value? Or am I required to add the shopId to the review document, too?



Solution 1:[1]

As far as I can see, you can get all orders of a specific shop, by querying like this:

var ordersRef = db.collection("orders");
var query = ordersRef.where("shopId", "==", "someId");

To get the corresponding reviews, you have to create a new reference that points to the review subcollection and read all reviews, a case in which there is no need to add the shopId as property within the review document.

If you wanted to use a collection group query, to get all reviews of a specific shop, then you would have needed that:

var reviews = db.collectionGroup("review").where("shopId", "==", "someId");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1