Reputation: 335
I have a collection called mouse
, which has inside several documents, which I have added by clicking somewhere on my file. The fields each document of this collection have are: 'type', 'position x', 'position y'
and 'timestamp'
.
I have used the following code in order to save each click on the db (in other words to put a new element in the collection):
var setData = ref.set({
'type': 'MC',
'x': xPosition,
'y': yPosition,
'timestamp': firebase.firestore.FieldValue.serverTimestamp()
})
and this code in order to print out all documents in the collection:
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? I appreciate any help you can provide
Upvotes: 1
Views: 1424
Reputation: 138834
My question is now, how could I delete all these documents without deleting the collection?
There is no way in which you can delete an entire collection without deleting all documents that exist under that particualr collection. So first delete all documents and the collection will be deleted automatically. In case of large collections I recommend you delete the documents in smaller chunks. This is happening client side, however you can write a function in Cloud Functions for Firebase to achieve the same thing server side.
Edit:
According to the official documentation regarding on how to delete collections using Javascript:
Deleting collections from a Web client is not recommended.
So if you have only a few documents, iterate through the collection and call delete()
on each document reference otherwise I recommend you Cloud Functions.
Upvotes: 2