How do you figure out how many documents are created per hour in a Firestore collection?

旧城冷巷雨未停 提交于 2019-12-11 10:56:53

问题


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.


回答1:


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

You can trigger this function using cron-job.org service.



来源:https://stackoverflow.com/questions/53507083/how-do-you-figure-out-how-many-documents-are-created-per-hour-in-a-firestore-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!