Executing collection group queries in a specific FireStore collection

走远了吗. 提交于 2020-06-17 13:08:47

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/62137927/executing-collection-group-queries-in-a-specific-firestore-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!