Reputation: 800
I have a database structure as follows (simplified for purposes of this question):
Collection: item_A
-> Document: params = {someParameter: "value"}
-> Document: user_01
-> Sub-collection: orders
-> Document: order_AA = {type: "A1", address: {pincode: "000000", city:"Paris"}
-> Document: order_AB = {type: "A2", address: {pincode: "111111", city:"London"}
...
-> Document: user_02
-> Sub-collection: orders
-> Document: order_AC = {type: "A1", address: {pincode: "222222", city:"Berlin"}
-> Document: order_AD = {type: "A1", address: {pincode: "333333", city:"Paris"}
...
Collection: item_B
-> Document: params = {someParameter: "value"}
-> Document: user_01
-> Sub-collection: orders
-> Document: order_BA = {type: "B1", address: {pincode: "000000", city:"Paris"}
-> Document: order_BB = {type: "B2", address: {pincode: "111111", city:"London"}
...
-> Document: user_02
-> Sub-collection: orders
-> Document: order_BC = {type: "B1", address: {pincode: "222222", city:"Berlin"}
-> Document: order_BD = {type: "B2", address: {pincode: "333333", city:"Paris"}
...
What I want to do: Obtain a list of all the orders across all users for a user-specified collection (item). (please note that the number of "user" documents is variable over time, and also the number of "order" documents within the sub-collection. The number of "items" is constant ~10, so I wish to avoid to manually code for handling each of them)
In more words... The user shall input the item type (e.g. item_A or item_B). Additionally the user can specify the required city (e.g. Paris). Now I want to query FireStore to find all the orders of that particular item type with the matching city.
How can I do this in the shortest steps (fewest queries)?
Can I use "collection group queries" for this? I have tried toying with this, but I am not sure I understand how. How can I restrict the query to run on a specified collection (e.g. item_A) and not all the available collections (item_A, item_B). Do I need to rename the sub-collections differently for each item (e.g. orders_item_A) to make use of collection group queries selectively?
Upvotes: 2
Views: 1428
Reputation: 598668
Update: it turns out that querying a specific path may be possible after all, thanks to how FieldPath.documentId()
is indexed for collection group indexes. Check @samthecodingman's answer here: https://stackoverflow.com/a/68049847
Collection group queries currently run on all collections of specific name. They cannot run on just the collections of a name in a specific path.
So you you want to run them on the orders under item_A
, you will have to give those subcollections a unique name, like item_A_orders
.
Upvotes: 3