Jak*_*ake 11 swift google-cloud-firestore
我正在寻找清除整个收藏夹的方法。我看到有一个批处理更新选项,但这需要我知道集合中的所有文档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)
}
Fra*_*len 17
没有 API 可以一次性删除整个集合(或其内容)。
要删除 Cloud Firestore 中的整个集合或子集合,请检索集合或子集合中的所有文档并将其删除。如果您有较大的集合,您可能希望以较小的批次删除文档以避免内存不足错误。重复该过程,直到您删除了整个集合或子集合。
该文档中甚至还有一个 Swift 示例,因此我建议您尝试一下。
Firebase CLI 允许您使用单个命令删除整个集合,但它只是调用 API 批量删除该集合中的所有文档。如果这适合您的需求,我建议您查看命令的(稀疏)文档firestore:delete。
Til*_*ddy 11
2020 更新答案
你可以用 Node JS 来做 - (注意他们使用了 Nodeprocess中一个著名的对象,在 Web javascript 中不可用)
查看由 firebase 托管的 Github上的这个片段。我总是把那个页面固定在我的浏览器上;)
// [START delete_collection]
async function deleteCollection(db, collectionPath, batchSize) {
const collectionRef = db.collection(collectionPath);
const query = collectionRef.orderBy('__name__').limit(batchSize);
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve).catch(reject);
});
}
async function deleteQueryBatch(db, query, resolve) {
const snapshot = await query.get();
const batchSize = snapshot.size;
if (batchSize === 0) {
// When there are no documents left, we are done
resolve();
return;
}
// Delete documents in a batch
const batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
await batch.commit();
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve);
});
}
// [END delete_collection]
Run Code Online (Sandbox Code Playgroud)
Keb*_*man 10
Firebase CLI中现在有一个选项可以删除整个Firestore数据库:
firebase firestore:delete --all-collections
Run Code Online (Sandbox Code Playgroud)
以下javascript函数将删除所有集合:
deleteCollection(path) {
firebase.firestore().collection(path).listDocuments().then(val => {
val.map((val) => {
val.delete()
})
})
}
Run Code Online (Sandbox Code Playgroud)
通过遍历每个文档并删除每个文档来工作。
另外,您可以使用Firestore的批处理命令,并使用以下功能一次删除所有命令:
deleteCollection(path) {
// Get a new write batch
var batch = firebase.firestore().batch()
firebase.firestore().collection(path).listDocuments().then(val => {
val.map((val) => {
batch.delete(val)
})
batch.commit()
})
}
Run Code Online (Sandbox Code Playgroud)
我发现删除所有文件的最干净的方法。我唯一会使用此功能的时间是在使用模拟器时,您只需将该功能粘贴到控制台中即可:
// Paste this in:
function clearCollection(path) {
const ref = firestore.collection(path)
ref.onSnapshot((snapshot) => {
snapshot.docs.forEach((doc) => {
ref.doc(doc.id).delete()
})
})
}
// Use it like this:
clearCollection('layers')
Run Code Online (Sandbox Code Playgroud)
如果您发现自己需要反复将这段代码保存为 Chrome 中的一个片段,那么您就可以轻松访问它,而不必一直将代码块粘贴到控制台中。您必须先运行代码段,然后才能从代码块访问它。文档
| 归档时间: |
|
| 查看次数: |
13860 次 |
| 最近记录: |