如果您将日期存储在 Firebase 中,这是可能的。有几种方法可以做到这一点,但无论您选择哪种方式,我都建议您在客户端之外进行。删除客户端潜在的大量数据并不是一个好主意。您可以从您控制的服务器设置 cron 作业。您定期运行作业并使用Admin SDK 访问和删除数据。
你会有一些针对文档的查询,比如这个例子:
let query = ref.collection('collectionname').where('timestamp', '<', {variable with the time frame goes here})
然后可以使用批量删除,如delete data section of the docs所示。
function deleteCollection(db, collectionPath, batchSize) {
var collectionRef = db.collection(collectionPath);
var query = collectionRef.where('timestamp', '<', {variable with the time frame goes here});
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, batchSize, resolve, reject);
});
}
function deleteQueryBatch(db, query, batchSize, resolve, reject) {
query.get()
.then((snapshot) => {
// When there are no documents left, we are done
if (snapshot.size == 0) {
return 0;
}
// Delete documents in a batch
var batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
return batch.commit().then(() => {
return snapshot.size;
});
}).then((numDeleted) => {
if (numDeleted === 0) {
resolve();
return;
}
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, batchSize, resolve, reject);
});
})
.catch(reject);
}
这只是一个一般示例,因此您必须根据您的数据结构和需求对其进行自定义,但它概述了这个想法。您可以在我上面包含的链接中查看更多信息。