如何删除firestore集合数据库中的所有文档

use*_*409 4 javascript google-cloud-firestore

我使用 Firestore 数据库来存储和检索数据。每天晚上 Firestore 集合中都需要有新的数据集(文档)可用。那么有没有办法一次性完全删除集合中的所有现有文档。

我尝试了那里的文档,它说我们需要一一删除,这是不可能的。因为文档ID是自动生成的。

以下是文档中的代码。

var cityRef = db.collection('cities').doc('BJ');

// Remove the 'capital' field from the document
var removeCapital = cityRef.update({
    capital: firebase.firestore.FieldValue.delete()
});
Run Code Online (Sandbox Code Playgroud)

那么有没有办法删除给定的 Fire Base 集合上的整个文档呢?

我尝试使用上面的代码,但出现以下错误。

(index):63 Uncaught ReferenceError: Firestore is not defined
    at myFunction ((index):63)
    at HTMLParagraphElement.onclick ((index):57)
Run Code Online (Sandbox Code Playgroud)

下面是我的代码:

<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>
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 9

文档是正确的:您必须单独删除文档。这并非不可能 - 您只需首先查询所有文档,然后删除每个文档即可。例如:

db.collection('cities').get().then(querySnapshot => {
    querySnapshot.docs.forEach(snapshot => {
        snapshot.ref.delete();
    })
})
Run Code Online (Sandbox Code Playgroud)