I'm trying to generate a weekly report from some data in Firebase database and using Firebase cloud function for that purpose. Timestamp actually exists in child nodes. However, I'm unable to fetch any data.
My DB structure is as follows :
{
"threads" : {
"228440-1704-4bba-87d7-27327" : {
"messages" : {
"8c98a76c-4456-4326-8e18-6d036b40bfa4" : {
"time" : 1519997400,
"payload": "someData"
},
"898376c-4456-4326-8e18-6deb4wr0bfa4" : {
"time" : 1517174415302,
"payload": "someData"
}
},
"details" : {
"created_At": 1514134435602
}
}
Fetch data on basis of detail node:
I'm trying to fetch threads where threads/details/created_at
exists in my date range (say last week).
Fetch data on basis of messages node: Apart from this, in another query, i want to fetch all messages (in different threads) for last week. I guess I'll have to fetch threads even in this scenario with some thing like:
//fetch threads where message.time exists in last week
I'm using Firebase cloud functions, and my code is as:
exports.generateReportFromThreads = functions.https.onRequest((req, res) => {
var ref = admin.database().ref('threads');
var startingTime = new Date('Mon, 25 Dec 1995 13:30:00 GMT').getUnixTime();
var currentDate = new Date('Fri, 2 Mar 2018 13:30:00 GMT').getUnixTime();
return ref.orderByChild('details/created_at').startAt(startingTime).endAt(currentDate).once("value", (threadSnapshot) => {
var numberOfThreads = threadSnapshot.numChildren()
var totalMessages = 0
threadSnapshot.forEach((snapshot) => {
var ref = snapshot.child('messages')
var messagesPerThread = ref.numChildren()
totalMessages = totalMessages + messagesPerThread
res.send(`
<!doctype html>
<html>
<body>
<p>Total number of threads = ${numberOfThreads}</p>
<p>Total messages = ${totalMessages}</p>
</body>
</html>`
);
});
});
I've applied indexes in my DB as: