如何使用 Google Cloud Function 有效删除旧的 Firebase Cloud 文档? [英] How to efficiently delete old Firebase Cloud documents using a Google Cloud Function?

查看:33
本文介绍了如何使用 Google Cloud Function 有效删除旧的 Firebase Cloud 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下计划的云功能来删除超过 24 小时的任何分配或职责(文档).功能正常,文档删除成功.

I am currently using the following scheduled Cloud Function to delete any Assignments or Duties (documents) older than 24 hours. The function works fine, and the documents successfully delete.

但是,该函数报告了大量的读取.似乎每一项任务或职责都在阅读",无论是否需要删除.我的理解是查询确实算作阅读,除非它们在使用的 querySnapshot 中返回一个文档?例如.任何被删除的文件都会有一个READ和DELETE.

However a HUGE amount of Reads are being reported by the function. It seems that every single assignment or duty is being "read", regardless of whether it needs to be deleted or not. It was my understanding that queries do count as reading, unless they return a document in the querySnapshot that is used? E.G. Any documents do be deleted will have a READ and DELETE.

有谁知道如何优化它以仅读取必须删除的文档?或者有没有更简单的方法可以使用 Google Cloud Function 删除旧的 Cloud Firebase 文档?我会认为这是一件很常见的事情.

Does anyone know how to optimize this to only read documents that must be deleted? Or is there an easier way to delete old Cloud Firebase documents using a Google Cloud Function? I would have thought this is a really common thing to do.

* 使用 collectionGroup 更新 #2 并正确使用 Promises(感谢您的帮助!)*

同样的问题:当函数运行时,它显示 844 Reads 和 1 Delete.绝对没有其他来源读取数据.峰值仅在运行函数后立即出现(使用 Google Cloud Scheduler - 立即运行)

Same problem: When the function is run, it is showing 844 Reads and 1 Delete. There is definitely no other sources reading the data. The spikes only occur right after running the function (using Google Cloud Scheduler - Run Now)

有趣的是,如果没有要删除的文档,那么就没有记录读取.但是如果有 > 1 个文档要删除,则有数百个 Reads.目前整个系列中只有大约 120 个职责!似乎每个职责都被阅读了多次!?

Interestingly, if there are NO documents to delete, then there are NO reads recorded. But if there is > 1 document to delete, there are hundreds of Reads. Currently there are only about 120 Duties in the whole collection! It seems each duty is being read multiple times!?

谢谢!

exports.scheduledCleanOldDutiesCollection = functions.pubsub.schedule('0 3 * * *')
    .timeZone('America/Chicago')
    .onRun((context) => {
        const dateYesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));   // 24 hours
        return db.collectionGroup('duties').where('date', '<', dateYesterday).get()
            .then(querySnapshot => {
                const promises = [];
                querySnapshot.forEach(doc => {
                    promises.push(doc.ref.delete());
                });
                return Promise.all(promises);
            });
    });

推荐答案

正如 Doug 在他的评论和官方 视频系列,您的代码必须返回一个仅在所有异步工作完成后才解析的承诺,否则云函数将提前终止.

As Doug explained in his comment and in the official video series, your code must return a promise that resolves only after all the asynchronous work is complete, else the Cloud Function will get terminated early.

您可以使用 Promise.all():

You can adapt your code as follows, by using Promise.all():

exports.scheduledCleanOldDutiesCollection = functions.pubsub.schedule('0 3 * * *')
    .timeZone('America/Chicago')
    .onRun((context) => {
        const dateYesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));   // 24 hours
        return db.collectionGroup('duties').where('date', '<', dateYesterday).get()
            .then(querySnapshot => {
                const promises = [];
                querySnapshot.forEach(doc => {
                    promises.push(doc.ref.delete());
                });
                return Promise.all(promises);
            });
    });

这篇关于如何使用 Google Cloud Function 有效删除旧的 Firebase Cloud 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆