java - 根据规则删除firestore文档
问题描述
我创建了一个应用程序,人们可以在其中上传一些自己的图像。
现在,为了处理人们可以上传不当图片的情况,我创建了一个报告系统。我所做的基本上是每次有人报告图像时,报告人的 ID 都会firestore
像这样添加到数组中:
db
.collection( "Reports" )
.document( ImageID )
.update( "Reports", FieldValue.arrayUnion( UID ) );
现在,我想设置一个规则,例如,如果数组的大小为 5(5 个不同的人报告图像),它将自动从云中删除该图像。
有什么办法可以代替每次读取数组并检查 ist 大小吗?
谢谢
解决方案
Reports
您可以在您的集合上创建触发器。
export const updateReportTrigger = functions.firestore
.document('Reports/{ImageID}')
.onUpdate(onUpdate)
async function onUpdate({ before, after }, context) {
const newData = after.data()
if (newData && newData.Reports && newData.Reports.length > 5) {
// put your delete logic here
// you can access the document id through context.params.ImageID
}
}
推荐阅读
- python - Python - 从子目录中捕获 txt 文件,无需手动编写
- php - 周数唯一可排序整数 id
- python - 使用 Doc2Vec 测量两个文档之间的相似性
- android - 根据文本值使用 kotlin 将数据从 firebase 检索到 android studio
- django - 如何在 Python Django 中托管 Angular 6 应用程序?
- java - Scope 如何影响 Bean 的对象?
- matlab - 为什么 eig() 的特征值是按升序排列的?
- r - 循环仅迭代前 4 行
- user-interface - 他们使用的是什么 3D(引擎)?
- java - 将字符串值与布尔值进行比较