Home > Software engineering >  Firestore query based on parent document fields
Firestore query based on parent document fields

Time:03-22

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?

CodePudding user response:

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");
  • Related