[英]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![]() |
invoices![]() |
inv1 ![]() |
inv2 ![]() |
|||
inv3 ![]() |
|||
user2![]() |
invoices![]() |
inv1 ![]() |
|
inv2 ![]() |
|||
inv3 ![]() |
|||
user3![]() |
invoices![]() |
inv1 ![]() |
|
inv2 ![]() |
|||
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:我能想到的几个场景:
All Users
.All Users
的新用户。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.