cpper
cpper

Reputation: 161

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?

Upvotes: 0

Views: 516

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

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

Upvotes: 1

Related Questions