简体   繁体   English

如何访问firestore中单个集合中多个文档的子集合?

[英]How to access sub-collections across multiple documents within a single collection in firestore?

I have a database structure as follows (simplified for purposes of this question):我有一个如下的数据库结构(为了这个问题而简化):

Collection: registrations
    -> Document: params = {someParameter: "value"}
    -> Document: user_01
        -> Sub-collection: orders
            -> Document: order_AA = {type: "Q1", address: {pincode: "000000", city:"Paris"}
            -> Document: order_AB = {type: "Q2", address: {pincode: "111111", city:"London"}
            ...
    -> Document: user_02
        -> Sub-collection: orders
            -> Document: order_AC = {type: "Q1", address: {pincode: "222222", city:"Berlin"}
            -> Document: order_AD = {type: "Q1", address: {pincode: "333333", city:"Paris"}
            ...

What I want to do: Obtain a list of all the orders across all users within the collection "registrations".我想要做什么:获取集合“注册”中所有用户的所有订单列表。 (please note that the number of "user" documents is variable over time, and also the number of "order" documents within the sub-collection) (请注意,“用户”文档的数量随时间变化,子集合中的“订单”文档的数量也是如此)

In more words... Query FireStore to find all the available documents (users) within the collection "registrations", and for each of these documents (users) find all the orders in their sub-collection named "orders".换句话说...查询 FireStore 以查找集合“registrations”中的所有可用文档(用户),并为每个文档(用户)在名为“orders”的子集合中找到所有订单。

How can I do this in the shortest steps (fewest queries)?我怎样才能以最短的步骤(最少的查询)做到这一点?

Also, is it essential for me to add a "dummy" field within each "user" document?另外,我是否必须在每个“用户”文档中添加一个“虚拟”字段? I have read on StackOverflow that FireStore cannot query sub-collections unless the parent document has "regular" fields.我在 StackOverflow 上读到 FireStore 无法查询子集合,除非父文档具有“常规”字段。 Even after adding a dummy field, I was unable to get it to work.即使添加了一个虚拟字段,我也无法让它工作。

My most recent effort has been:我最近的努力是:

let ordersList = [];
await firestore()
    .collection("registrations")
    .get()
    .then((collectionRef) => {          
        collectionRef.forEach((userDoc) => {
            console.log("==", userDoc.id);
            if(userDoc.id.startsWith("user")) {
                userDoc.collections("orders")
                    .get()
                    .then((ordersSnapshot) => {
                        ordersSnapshot.forEach((orderDoc) => {
                            ordersList.push(orderDoc.data());
                        });
                    });
            }
        });
    })
    .catch(error => {
        console.log("Error in retrieving data!");
        console.log('Error: ', error);
    })

I get the error:我得到错误:

Error: [TypeError: userDoc.collections is not a function.错误:[TypeError:userDoc.collections 不是 function。 (In 'userDoc.collections("orders")', 'userDoc.collections' is undefined)] (在 'userDoc.collections("orders")' 中,'userDoc.collections' 未定义)]

You should definitely use the collectionGroup() method as described there .您绝对应该使用 那里描述的collectionGroup()方法。

You can use a collection group query to query all documents among collections and subcollections with the same name.您可以使用集合组查询来查询 collections 和同名子集合中的所有文档。 So, for the subcollection "orders", you can query all documents in those collections using:因此,对于子集合“订单”,您可以使用以下命令查询 collections 中的所有文档:

firestore().collectionGroup("orders").get()

If you don't want all of the order documents, you will need to add a filter in the same way that you would any other query.如果您不想要所有订单文档,则需要以与任何其他查询相同的方式添加过滤器。 You can only filter using the fields available in each orders document (you can't filter per user unless you have a "user" field in the document to use).您只能使用每个订单文档中可用的字段进行过滤(除非您在文档中有“用户”字段可供使用,否则您无法按用户进行过滤)。

I have read on StackOverflow that FireStore cannot query sub-collections unless the parent document has "regular" fields我在 StackOverflow 上读到 FireStore 无法查询子集合,除非父文档具有“常规”字段

This is not true.这不是真的。 There is no need to have a parent document in place to query its nested subcollections.不需要有一个父文档来查询它的嵌套子集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM