简体   繁体   English

从 Firestore 中的多个子集合的多个子集合中获取所有文档的最有效方法是什么?

[英]What is the most efficient way to get all documents from multiple sub-collections of multiple sub-collections in Firestore?

Let's say I have a collection with a structure as such:假设我有一个结构如下的集合:

Collection收藏 Documents文件 Sub-Collection子集 Documents文件
users用户 user1用户1 invoices发票 inv1 inv1
inv2 inv2
inv3 inv3
user2用户2 invoices发票 inv1 inv1
inv2 inv2
inv3 inv3
user3用户3 invoices发票 inv1 inv1
inv2 inv2
inv3 inv3

Let's say I have over 200 users with each user having over 500 invoices.假设我有 200 多个用户,每个用户有 500 多张发票。

I am using Firestore as a backend for my ReactJS app.我正在使用 Firestore 作为我的 ReactJS 应用程序的后端。 So I currently have the ability to pull each user's invoices when I access their userId .因此,我目前可以在访问每个用户的userId时提取他们的发票。 However, I want to have a new feature on a new page where I can access ALL INVOICES at once.但是,我想在新页面上有一个新功能,我可以在其中一次访问所有发票。 So what is the most efficient way for me to do this?那么对我来说最有效的方法是什么?

A couple of scenarios that I could think of:我能想到的几个场景:

  1. Create a cloud function where it compiles all Invoices from each user into a single new user with the name All Users .创建一个云 function ,它将每个用户的所有发票编译成一个名为All Users的新用户。
  2. Iterate through all the userId to get all of the invoices from each user.遍历所有userId以获取每个用户的所有发票。

I want to have a new feature on a new page where I can access ALL INVOICES at once.我想在新页面上有一个新功能,我可以在其中一次访问所有发票。 So what is the most efficient way for me to do this?那么对我来说最有效的方法是什么?

If you want to get all invoices of all users, then you should consider using a collection group query .如果你想得到所有用户的所有发票,那么你应该考虑使用集合组查询

Since you say that:既然你这么说:

Let's say I have over 200 users with each user having over 500 invoices.假设我有 200 多个用户,每个用户有 500 多张发票。

Then we speak about 100k documents, which is way too much to get in a single go. For that, I recommend you get the data in smaller chunks by implementing pagination .然后我们谈论 100k 文档,这对于单个 go 来说太多了。为此,我建议您通过实施分页以较小的块来获取数据。

Regarding the first scenario, it will work, but it doesn't make any sense to do that since we have collection group queries.关于第一种情况,它会起作用,但这样做没有任何意义,因为我们有集合组查询。

Regarding the second scenario, it will also work but you need to store somehow all user IDs and then perform a separate query for each user.关于第二种情况,它也可以工作,但您需要以某种方式存储所有用户 ID,然后为每个用户执行单独的查询。 It complicates things a bit.这让事情变得有点复杂。

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

相关问题 读取文档 firestore 的子集合中的文档(V9(模块化)) - Read documents in the sub-collections of a document firestore (V9 (Modular)) 在子集合更新时触发云 function - Trigger cloud function on sub-collections update SwiftUI 和 Firestore - 启动时从不同 collections 读取多个文档的逻辑 - SwiftUI and Firestore - logic for reading multiple documents from different collections at startup 使用子 collections >> 子文档从 firebase 文档读取数据 - Read data from firebase document with sub collections >> sub documents 如何获取所有 Firestore Collections 和文档? | 反应JS - How to Get all Firestore Collections and Documents? | React JS 多个 collections 在 1 个批量写入 firestore - Multiple collections in 1 batched write firestore 获取 Firestore 中的所有 collections - Fetching all collections in Firestore Nex.js getStaticProps——等待多个 Firestore collections 并返回全部作为道具? - Nex.js getStaticProps -- await multiple Firestore collections and return all as props? 无法从 FIRESTORE 的集合中获取多个文档 - UNABLE TO Get multiple documents from a collection FROM FIRESTORE 如何从Flutter上的Firestore获取collections的列表和一个集合下所有字段的列表? - How to get a list of collections and a list of all fields under a collection from Firestore on Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM