如何使用Google Cloud Function有效删除旧的Firebase Cloud文档?

问题描述 投票:0回答:1

我当前正在使用以下计划的Cloud Function删除24小时之前的所有工作分配或职责(文档)。该功能运行正常,并且文档已成功删除。

但是,此功能报告的读取次数]很大。似乎“ [[每一个]]”分配或职责正在“读取”,无论是否需要删除它。据我了解,除非查询返回使用的querySnapshot中的文档,否则查询确实算作阅读?例如。任何要删除的文档将具有读取和删除的权限。有人知道如何优化此方法以仅读取必须删除的文档吗?还是有使用Google Cloud Function删除旧Cloud Firebase文档的简便方法?我本以为这是一件很普通的事情。

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

同一问题:运行该函数时,它显示844次读取和1次删除。绝对没有其他来源读取数据。尖峰仅在运行功能后立即发生(使用Google Cloud Scheduler-立即运行)

Reads Spike right after the function runs

enter image description here

谢谢!

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

我当前正在使用以下计划的Cloud Function删除24小时之前的所有工作分配或职责(文档)。该功能运行正常,并且文档已成功删除。 ...
javascript node.js firebase google-cloud-firestore google-cloud-functions
1个回答
1
投票
如Doug在其评论和官方video series中所述,您的代码必须返回仅在所有异步工作完成后才能解决的承诺,否则Cloud Function会提早终止。

您可以通过使用Promise.all()来如下修改代码:

© www.soinside.com 2019 - 2024. All rights reserved.