2

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?

  • This question sounds essentially the same as your other one: https://stackoverflow.com/q/62137635/807126 – Doug Stevenson Jun 01 '20 at 17:53
  • They have similar background, but different I believe. I originally needed to do this and was having trouble with collection group queries, and so decided to shift to something (seemingly) simpler to start with (the other post). This post is asking specific queries about how can I restrict the scope of collection group queries. Could you please take a look again and see if you can offer any more advice. Thanks in advance. – Shailesh Appukuttan Jun 01 '20 at 17:57
  • I don't have anything to add. My answer in the other question is complete. – Doug Stevenson Jun 01 '20 at 18:04
  • Thanks for your time. Your response certainly resolves the other post, but I feel that solution would not suffice (in that form) for this problem here. Specifically, "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)"? – Shailesh Appukuttan Jun 01 '20 at 18:07
  • Well, you can't. You add fields to documents that you can use for collection group filtering, as I suggested in my other answer. – Doug Stevenson Jun 01 '20 at 18:09

1 Answers1

3

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks a lot for you comment. I have now renamed all my sub-collections to be uniquely identifiable, and everything works fine. But I am now faced with a problem of filtering my collection group query. As the question was looking messy to post here in the comments, I have made a fresh post for the same: https://stackoverflow.com/questions/62157759/filter-a-collection-group-query-in-firestore Can you see if you can help? Thanks in advance. – Shailesh Appukuttan Jun 02 '20 at 17:25