您如何确定每小时在Firestore集合中创建多少个文档? [英] How do you figure out how many documents are created per hour in a Firestore collection?
问题描述
找出每小时要在Firestore集合中创建多少文档的最佳方法是什么.我创建了一个云函数,该函数在每次添加或删除文档时都会进行计数,但是我似乎找不到一种方法来计算发生这种情况的速度.
What is the best way to figure out how many documents are being created per hour into a Firestore collection. I have created a cloud functions which counts each time a document is added or removed but I can't seem to find a way to figure out the rate at which this is occurring.
推荐答案
要解决此问题,应在集合中的每个文档中添加类型为Date
的新属性,该属性应包含创建日期和时间.现在,您可以在 Firebase的云函数中创建一个函数,该函数将添加到以下位置您的数据库在过去一小时内添加的文档数.根据我在
To solve this, you should add to each document in your collection a new property of type Date
, that should hold the date and time of its creation. Now you can create a function, in Cloud Functions for Firebase that will add to a location in your database the number of documents added in the last hour. You can write this number in a Firebase realtime database rather than in Cloud Firestore, according to the last part of my answer within this post.
该函数实际上应该使用如下所示的查询来计算文档数:
The function should actually count the number of documents using a query that look like this:
var today = new Date();
var lastHour = date.setDate(today.getDate() - 3600);
db.collection("nameOfCollection").where("date", ">", lastHour);
您可以使用 cron-job.org 服务触发该功能.
You can trigger this function using cron-job.org service.
这篇关于您如何确定每小时在Firestore集合中创建多少个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!