如何查询Firestore今天所有日期?

时间:2019-12-12 09:01:22

标签: node.js firebase google-cloud-firestore google-cloud-functions

我正在尝试按固定的时间表生成报告。但是我遇到了一个问题,在该函数运行的当前时间之前,我无法检索今天开始的日期。

exports.generateReport = functions.pubsub.schedule('36 15 * * *').onRun(async (context) => {
    console.log(context);
    const currentTime = admin.firestore.Timestamp.now();

    const shopSnapshot = await db.collection("shops").get();
    let shopDoc = shopSnapshot.docs.map(doc => doc.data());

    const promises = [];
    let transactionList = [];
    let reportList = [];

    let i = 0;

    console.log(shopDoc);

    shopDoc = shopDoc.filter(shop => !!shop.key);
    console.log(shopDoc);

    for(var j=0; j<shopDoc.length; j++){
        console.log("Enter shop ID:"+shopDoc[j].key);
        promises.push(db.collection("shops").doc(shopDoc[j].key).collection("transactions").get());
    }  

    const snapshotArrays = await Promise.all(promises);

    snapshotArrays.forEach(snapArray => {
        snapArray.forEach(snap => {
            //console.log(snap.data());
            transactionList.push({data: snap.data(), key: shopDoc[i].key});
        })
        i++;
    }); 

    for(var k=0; k<shopDoc.length; k++){
        let amount = 0;
        for (var l=0; l<transactionList.length; l++){
            if(shopDoc[k].key === transactionList[l].key){
                console.log("get date");

                if (transactionList[l].data.createAt < currentTime){
                    amount += transactionList[l].data.amount;
                    console.log(amount);
                }
            }
        }
        reportList.push({amount: amount, key: shopDoc[k].key});
    }

    console.log(reportList);
    console.log(transactionList);

});

This is Firestore document and collection images

我尝试使用new Date()并与一串与Firestore时间戳格式完全相同的日期进行了比较,但所有交易仍在该时间之前出现,或者此时不包括任何交易。

1 个答案:

答案 0 :(得分:3)

例如,如果我正确理解您想通过今天在00:05运行调度Cloud Function来为昨天发生的所有交易生成每日报告,则可以使用moment.js库:

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

const moment = require('moment');

admin.initializeApp();


exports.generateReport = functions.pubsub.schedule('05 00 * * *').onRun(async (context) => {

    let m1 = moment();
    let m2 = moment();
    m1.add(-1, 'days');
    m2.add(-1, 'days');
    m1.startOf('day');
    m2.endOf('day');


    const shopSnapshot = await db.collection("shops").get();
    let shopDoc = shopSnapshot.docs.map(doc => doc.data());

    const promises = [];
    let transactionList = [];
    let reportList = [];

    let i = 0;

    shopDoc = shopDoc.filter(shop => !!shop.key);
    console.log(shopDoc);

    for(var j=0; j<shopDoc.length; j++){
        console.log("Enter shop ID:"+shopDoc[j].key);
        promises.push(
          db.collection("shops")
            .doc(shopDoc[j].key)
            .collection("transactions")
            .orderBy("createAt")
            .where("createAt", ">", m1.toDate())
            .where("createAt", "<=", m2.toDate())
            .get()
       );
    }  

    const snapshotArrays = await Promise.all(promises);

    snapshotArrays.forEach(snapArray => {
        snapArray.forEach(snap => {
            //console.log(snap.data());
            transactionList.push({data: snap.data(), key: shopDoc[i].key});
        })
        i++;
    }); 

    for(var k=0; k<shopDoc.length; k++){
        let amount = 0;
        for (var l=0; l<transactionList.length; l++){
            if(shopDoc[k].key === transactionList[l].key){
                 amount += transactionList[l].data.amount;
            }
        }
        reportList.push({amount: amount, key: shopDoc[k].key});
    }

    //.....

});

那么,我们在这段代码中做什么? 首先,我们创建两个矩对象,并将它们的日期设置为昨天。然后,使用startOf()endOf()将第一个时间调整为昨天的时间,即上午12:00:00.000(即00:00:00,请参见here),第二个时间到昨天晚上11:59:59.999(即23:59:59)。

对于这两个日期,对于每个交易集合,我们将query修改如下,调用toDate()方法:

db.collection("shops")
                .doc(shopDoc[j].key)
                .collection("transactions")
                .orderBy("createAt")
                .where("createAt", ">", m1.toDate())
                .where("createAt", "<=", m2.toDate())
                .get();

就是这样。这里最大的优点是,过滤是在Firestore数据库中(在后端)完成的,而不是在您的问题(if (transactionList[l].data.createAt < currentTime){...})中完成的