javascript - 如何删除firestore集合数据库中的所有文档
问题描述
我使用 Firestore 数据库来存储和检索数据。每天晚上,Firestore 集合中都需要有可用的新数据集(文档)。那么有什么方法可以一次完全删除集合中的所有现有文档。
我试过那里的文档,它说我们需要一个一个地删除,这是不可能的。因为文档 ID 是自动生成的。
以下是来自documantaion的代码。
var cityRef = db.collection('cities').doc('BJ');
// Remove the 'capital' field from the document
var removeCapital = cityRef.update({
capital: firebase.firestore.FieldValue.delete()
});
那么有什么方法可以删除给定火力基地集合中的整个文档?
我尝试使用上面的代码,但出现以下错误。
(index):63 Uncaught ReferenceError: Firestore is not defined
at myFunction ((index):63)
at HTMLParagraphElement.onclick ((index):57)
下面是我的代码:
<p id="demo" onclick="myFunction()">Click me.</p>
<script>
function myFunction() {
describe("firestore", () => {
var db;
before(() => {
var config = {
apiKey: "xxxx",
aauthDomain: "xxxxfirebaseapp.com",
projectId: "xxx",
};
var app = firebase.initializeApp(config);
db = firebase.firestore(app);
//firebase.firestore.setLogLevel("debug");
});
db.collection('Headings').get().then(querySnapshot => {
querySnapshot.docs.forEach(snapshot => {
snapshot.ref.delete();
})
})
}
</script>
解决方案
推荐阅读
- user-interface - 在 Cooja 中运行脚本时收到错误消息
- c - 读取文件时如何获取并打印最后一个单词?
- javascript - Setting up Ajax Promise
- bash - 如何在 Linux shell 脚本中提示是/否?
- javascript - 在组件中单击链接时如何关闭 react-burger-menu?
- reactjs - 为什么此代码段中的 count 值周围有括号?
- python - 我的斐波那契生成器给出的结果不正确
- flutter - 带有标头和其他字段的 Flutter Multipart Request
- c# - 组合框中留下的虚假值
- linux-kernel - 理论问题:从 128 位可寻址内存中获取 32 位变量