如何访问firestore中单个集合中跨多个文档的子集合? [英] How to access sub-collections across multiple documents within a single collection in firestore?

查看:50
本文介绍了如何访问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)

换句话说...查询 FireStore 以查找registrations"集合中的所有可用文档(用户),并针对这些文档(用户)中的每一个查找其名为orders"的子集合中的所有订单.

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

我怎样才能以最短的步骤(最少的查询)做到这一点?

How can I do this in the shortest steps (fewest queries)?

另外,我是否必须在每个用户"文档中添加一个虚拟"字段?我在 StackOverflow 上读到 FireStore 无法查询子集合,除非父文档具有常规"字段.即使添加了一个虚拟字段,我也无法让它工作.

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. Even after adding a dummy field, I was unable to get it to work.

我最近的努力是:

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);
    })

我收到错误:

错误:[类型错误:userDoc.collections 不是函数.(在'userDoc.collections("orders")', 'userDoc.collections' 未定义)]

Error: [TypeError: userDoc.collections is not a function. (In 'userDoc.collections("orders")', 'userDoc.collections' is undefined)]

推荐答案

您可以使用 集合组查询 查询同名集合和子集合中的所有文档.因此,对于子集合订单",您可以使用以下命令查询这些集合中的所有文档:

You can use a collection group query to query all documents among collections and subcollections with the same name. So, for the subcollection "orders", you can query all documents in those collections using:

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

我在 StackOverflow 上读到 FireStore 无法查询子集合,除非父文档具有常规"字段

I have read on StackOverflow that FireStore cannot query sub-collections unless the parent document has "regular" fields

这不是真的.无需使用父文档来查询其嵌套的子集合.

This is not true. There is no need to have a parent document in place to query its nested subcollections.

这篇关于如何访问firestore中单个集合中跨多个文档的子集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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