我正在寻找清除整个收藏夹的方法。我看到有一个批处理更新选项,但这需要我知道集合中的所有文档ID。
我正在寻找一种简单地删除集合中每个文档的方法。
谢谢!
编辑:下面的答案是正确的,我使用以下方法:
func delete(collection: CollectionReference, batchSize: Int = 100) {
// Limit query to avoid out-of-memory errors on large collections.
// When deleting a collection guaranteed to fit in memory, batching can be avoided entirely.
collection.limit(to: batchSize).getDocuments { (docset, error) in
// An error occurred.
let docset = docset
let batch = collection.firestore.batch()
docset?.documents.forEach { batch.deleteDocument($0.reference) }
batch.commit {_ in
self.delete(collection: collection, batchSize: batchSize)
}
}
Run Code Online (Sandbox Code Playgroud)
}
我需要一种方法来快速清除整个 firestore 数据库,但找不到有关如何执行此操作的详细文档。最终我在堆栈溢出上找到了这个答案(清除所有数据的 Firestore 数据库?),但对清除数百万个文档的整个数据库需要多长时间感到有点紧张,所以我想要一种方法来递归删除集合一次。
背景:我一直在运行一些测试,将大量数据从旧数据库迁移到 firestore,每次运行后我都希望在 firestore 中使用干净的状态。不要在生产数据上使用它!