从 Firebase 的 Cloud Firestore 中删除文档是否会删除该文档中的任何子集合?
Does deleting a document from Firebase's Cloud Firestore delete any sub collections in that document?
这是我当前如何删除文档的示例:
let transactionsRef = db.collection(Globals.GroupsPath).document(Group.instance.getAll().id).collection(Globals.GroupTransactions)
let query = transactionsRef.whereField(Globals.TransactionCreator, isEqualTo: Auth.auth().currentUser!.uid)
query.getDocuments { (snapshot, error) in
guard error == nil else {
print(error!.localizedDescription)
callback(error)
return
}
if let snapshot = snapshot {
let batch = self.db.batch()
for doc in snapshot.documents {
batch.deleteDocument(doc.reference)
}
batch.commit()
callback(nil)
}
// TODO: Create an error here and return it.
}
但是我注意到,在执行此操作后,Firestore 数据库中的文档变灰了,但我仍然可以单击它并查看该文档中的集合及其数据!
我是否需要在删除父文档之前手动删除子集合中的每一项,还是只需要一段时间才能完成删除?这是怎么回事?
删除文档不会删除子集合。您确实需要手动删除所有子集合,如文档中所述 here。您会看到文档不建议从客户端删除子集合,因为有很多方法可能出错。对于客户端来说是labor-intensive,可能会涉及到读写权限问题等。你会想要使用服务器或无服务器解决方案。因此,例如,这就是删除子集合 server-side 与 Node.js:
的工作方式
function deleteCollection(db, collectionPath, batchSize) {
var collectionRef = db.collection(collectionPath);
var query = collectionRef.orderBy('__name__').limit(batchSize);
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);
}
这是我当前如何删除文档的示例:
let transactionsRef = db.collection(Globals.GroupsPath).document(Group.instance.getAll().id).collection(Globals.GroupTransactions)
let query = transactionsRef.whereField(Globals.TransactionCreator, isEqualTo: Auth.auth().currentUser!.uid)
query.getDocuments { (snapshot, error) in
guard error == nil else {
print(error!.localizedDescription)
callback(error)
return
}
if let snapshot = snapshot {
let batch = self.db.batch()
for doc in snapshot.documents {
batch.deleteDocument(doc.reference)
}
batch.commit()
callback(nil)
}
// TODO: Create an error here and return it.
}
但是我注意到,在执行此操作后,Firestore 数据库中的文档变灰了,但我仍然可以单击它并查看该文档中的集合及其数据!
我是否需要在删除父文档之前手动删除子集合中的每一项,还是只需要一段时间才能完成删除?这是怎么回事?
删除文档不会删除子集合。您确实需要手动删除所有子集合,如文档中所述 here。您会看到文档不建议从客户端删除子集合,因为有很多方法可能出错。对于客户端来说是labor-intensive,可能会涉及到读写权限问题等。你会想要使用服务器或无服务器解决方案。因此,例如,这就是删除子集合 server-side 与 Node.js:
的工作方式function deleteCollection(db, collectionPath, batchSize) {
var collectionRef = db.collection(collectionPath);
var query = collectionRef.orderBy('__name__').limit(batchSize);
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);
}