[英]Executing collection group queries in a specific FireStore collection
我有一个如下的数据库结构(为了这个问题而简化):
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,所以我希望避免手动编码用于处理它们中的每一个)
换句话说... 用户应输入项目类型(例如 item_A 或 item_B)。 此外,用户可以指定所需的城市(例如巴黎)。 现在我想查询 FireStore 以查找具有匹配城市的特定项目类型的所有订单。
我怎样才能以最短的步骤(最少的查询)做到这一点?
我可以为此使用“集合组查询”吗? 我试过玩弄这个,但我不确定我是怎么理解的。 如何限制查询在指定集合(例如 item_A)上运行,而不是在所有可用的 collections(item_A、item_B)上运行。 我是否需要为每个项目(例如orders_item_A)以不同的方式重命名子集合以有选择地使用集合组查询?
更新:事实证明,查询特定路径毕竟是可能的,这要归功于FieldPath.documentId()
是如何为集合组索引编制索引的。 在此处查看@samthecodingman 的答案: https://stackoverflow.com/a/68049847
集合组查询当前在特定名称的所有 collections 上运行。 它们不能仅在特定路径中名称的 collections 上运行。
所以你想在item_A
下的订单上运行它们,你必须给这些子集合一个唯一的名称,比如item_A_orders
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.