简体   繁体   English

对一个或多个 Firestore 集合中的文档进行计数的最有效方法

[英]Most efficient approach to count documents in one or more Firestore collection(s)

I'm developing a delivery application, I would like to get the counts for the admin to analyze (number of orders, number orders paid with visa, etc...).我正在开发一个交付应用程序,我想获取管理员分析的计数(订单数量,通过签证支付的订单数量等......)。 I tried to read all the documents and get snapshot count but I figured out it will cost me a lot of reads and if the dataset is large it will slow down my application.我试图阅读所有文档并获取快照计数,但我发现这会花费我大量的阅读时间,如果数据集很大,它会减慢我的应用程序。 So I created a collection called counts, and I created a cloud function to listen for document create and increment the count field in users document in the counts' collection, it works perfectly but I would like to know if it will cost me a fortune and if there is a better solution.因此,我创建了一个名为 counts 的集合,并创建了一个云 function 来侦听文档创建并增加计数集合中用户文档中的计数字段,它运行良好,但我想知道它是否会花费我一大笔钱如果有更好的解决方案。 note: I will listen to 5+ collections注意:我会听 5+ collections

here is my function:这是我的 function:

const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

    var firestore = admin.firestore();
 // Listens for any document created in the users collection
exports.createUser = functions.firestore
    .document('users/{userId}')
    .onCreate((snap, context) => {
 firestore.collection('counts').doc('users').update({ count: admin.firestore.FieldValue.increment(1) })
return 'done';
});

I created a cloud function to listen for document create and increment the count field in users document in the counts' collection.我创建了一个云 function 来监听文档创建并增加计数集合中用户文档中的计数字段。

This is indeed one of the best solution for your requirement.这确实是满足您要求的最佳解决方案之一。 Note that if the frequency of write to the "main collections" (ie the 5+ collections) is so high that the counter document may be updated more that once per second, you may use a set of distributed counters .请注意,如果写入“主集合”(即 5 个以上集合)的频率非常高,以至于计数器文档的更新次数可能超过每秒一次,您可以使用一组 分布式计数器

I would like to know if it will cost me a fortune我想知道这是否会花费我一大笔钱

Each time you create a document, the cost for updating the counter corresponds to one Firestore write and one Cloud Function invocation (and potentially some Outbound networking cost depending on where are located your Firestore database and your Cloud Function, see here and here ).每次创建文档时,更新计数器的成本对应于一次 Firestore 写入和一次 Cloud Function 调用(可能还有一些出站网络成本,具体取决于您的 Firestore 数据库和 Cloud Function 的位置,请参见此处此处)。 Will it cost "a fortune"?会不会花“一大笔钱”? It all depends on the number of documents created in the "main collections" AND the number of times you want to read these counters.这完全取决于在“主集合中创建的文档数量以及您想要读取这些计数器的次数。

If you read these counters once a year , it may be more affordable to query all the documents of the 5 collections.如果你一年读一次这些计数器,查询 5 collections 的所有文档可能会更实惠。 But if you read it once a day or once a week and those collections have "a lot of documents", it will probably be more affordable to use a Cloud Function as you are doing.但是,如果您每天或每周阅读一次,并且那些 collections 有“很多文档”,那么使用云 Function 可能会更实惠。 It's up to you to do the math in order to evaluate the preferred approach, based on some figures that you are the only one to know (or the only one being able to estimate).根据您是唯一知道(或唯一能够估计的)的一些数字,您可以进行数学计算以评估首选方法。

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

相关问题 如何使用Cloud Firestore集合中的文件数量及其ID计数 - How to get a count of the number of documents and the id's of these, in a collection with Cloud Firestore 在多个文档上进行Firestore交易是否比逐个更新每个文档更有效? - Are Firestore transactions on multiple documents more efficient than updating each document one by one? 如何计算 Firestore 集合下的文档数量? - How to count the number of documents under a collection in Firestore? 在 Cloud Firestore 中处理大型集合的过滤和排序的最有效和可扩展的方法是什么? - What's the most efficient and scalable way to handle filtering and sorting of a large collection in cloud firestore? 从 Firestore 的一个集合中获取所有文档 - Getting all documents from one collection in Firestore Android Cloud Firestore 从集合中获取少量文档的有效方法 - Android Cloud Firestore efficient way to get few documents from collection Firestore云功能 - 保留集合中的文档数量 - Firestore Cloud Functions - Keeping Count of Amount of Documents in Collection 如何使用 Cloud Firestore 获取集合中文档的数量 - How to get a count of number of documents in a collection with Cloud Firestore 在多个 firestore 集合中使用 orderby - Use orderby in more than one firestore collection 使用 Firestore 获取文档集合 - get collection of documents with firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM