问题
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