我有一个名为 mouse
的集合,其中包含多个文档,这些文档是我通过单击文件上的某个位置添加的。该集合中每个文档的字段为:'type'、'position x'、'position y'
和 'timestamp'
。
我使用了以下代码来保存对数据库的每次点击(换句话说,将一个新元素放入集合中):
var setData = ref.set({
'type': 'MC',
'x': xPosition,
'y': yPosition,
'timestamp': firebase.firestore.FieldValue.serverTimestamp()
})
此代码用于打印集合中的所有文档:
db.collection("mouse").where("type", "==", "MC")
.onSnapshot(function(querySnapshot) {
var dataPoints = [];
querySnapshot.forEach(function(doc) {
dataPoints.push(doc.data().type + " " + doc.data().x + " " + doc.data().y + " " + doc.data().timestamp);
});
console.log("Current data in database: ", dataPoints.join(", ") + "\n");
});
现在我的问题是,如何删除所有这些文档而不删除集合?感谢您提供的任何帮助
最佳答案
My question is now, how could I delete all these documents without deleting the collection?
您无法在不删除特定集合下存在的所有文档的情况下删除整个集合。所以先删除所有文档,集合就会自动删除。如果集合很大,我建议您以较小的 block 删除文档。这是在客户端发生的,但是您可以在 Cloud Functions for Firebase 中编写一个函数在服务器端实现同样的事情。
编辑:
根据关于how to delete collections的官方文档使用 JavaScript:
Deleting collections from a Web client is not recommended.
因此,如果您只有几个文档,请迭代集合并对每个文档引用调用 delete()
,否则我建议您使用 Cloud Functions。
关于javascript - 如何使用 Javascript 删除集合中的所有文档而不删除 firebase 中的集合本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55709046/