在特定的FireStore集合中执行集合组查询 [英] Executing collection group queries in a specific FireStore collection

查看:60
本文介绍了在特定的FireStore集合中执行集合组查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有如下数据库结构(出于这个问题的目的而简化):

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"}
            ...

我要做什么:获取所有用户针对用户指定的收藏集(项目)的所有订单的列表.(请注意,用户"文档的数量随时间变化,子集合内订单"文档的数量也是可变的.项目"的数量恒定为〜10,因此,我希望避免手动编码用于处理它们中的每一个)

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)

换句话说...用户应输入项目类型(例如item_A或item_B).此外,用户可以指定所需的城市(例如巴黎).现在,我想查询FireStore以查找具有匹配城市的该特定项目类型的所有订单.

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)?

我可以使用收藏组查询" 为此?我尝试玩弄这个,但是我不确定我怎么理解.如何限制查询在指定的集合(例如item_A)上运行,而不是在所有可用的集合(item_A,item_B)上运行.我是否需要为每个商品(例如,orders_item_A)以不同的方式对子集合进行重命名,以便有选择地利用集合组查询?

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?

推荐答案

集合组查询当前在所有具有特定名称的集合上运行.它们不能仅在特定路径中的名称集合上运行.

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.

因此,您要在 item_A 下的订单上运行它们,则必须给这些子集合一个唯一的名称,例如 item_A_orders .

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.

这篇关于在特定的FireStore集合中执行集合组查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆