我有一个flutter应用程序,该应用程序基本上是另一个应用程序的过滤器应用程序,这意味着我可以滚动浏览某些帖子,并决定是否将其从此应用程序中删除,以便它们不会显示在其他主要应用程序上。
我的问题是,由于Firestore不支持删除子集合,如果我只删除帖子的文档,而忽略诸如评论之类的其余子集合,会发生什么情况? firestore是否有可能在以后分配一个与先前删除的postId相同的随机postId并最终显示删除的帖子的评论和子集合信息?因为在Firestore上说对于查询中不会显示的子集合不存在的祖先文档,这是否意味着不会使用相同的postId创建其他帖子?
基本上,不删除子集合有什么危害,如果您建议我对此做些什么,请手动删除它?
答案 0 :(得分:4)
在删除文档时,可以使用firebase函数删除集合。换句话说,您将编写一个函数,该函数在每次删除文档(在您的情况下为帖子)时执行。然后,您将遍历子集合并在函数中将其删除。
要删除集合,请使用以下代码(我没有对此进行编码):
/**
* Delete a collection, in batches of batchSize. Note that this does
* not recursively delete subcollections of documents in the collection
*/
function deleteCollection (db, collectionRef, batchSize) {
var query = collectionRef.orderBy('__name__').limit(batchSize)
return new Promise(function (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(function (doc) {
batch.delete(doc.ref)
})
return batch.commit().then(function () {
return snapshot.size
})
}).then(function (numDeleted) {
if (numDeleted <= batchSize) {
resolve()
return
}
else {
// Recurse on the next process tick, to avoid
// exploding the stack.
return process.nextTick(function () {
deleteQueryBatch(db, query, batchSize, resolve, reject)
})
}
})
.catch(reject)
}
答案 1 :(得分:3)
firestore以后是否有可能分配一个与先前删除的postId相同的随机postId
在这种情况下,id的冲突极不可能发生,您可以/应该假定它们将是完全唯一的。因此,您不必担心它,因为这就是为什么这些id是唯一的。
当您在不传递任何参数的情况下调用CollectionReference的add()
方法或CollectionReference的document()
方法时,Firestore中使用的唯一ID的内置生成器会生成随机且高度不可预测的ID,从而避免了后端基础架构中的热点。
这是否意味着不会使用相同的postId创建其他帖子?
是的,将不会创建其他具有相同ID的文档。
基本上,删除子集合不会有任何危害
没有。您可以通过两种方式来做到这一点,即在客户端获取该子集合中的所有文档,然后以较小的块将其删除,或者使用其答案中提到的@ jonasxd360函数。