Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete document from firestore using where clause

var jobskill_ref = db.collection('job_skills').where('job_id','==',post.job_id); jobskill_ref.delete(); 

Error thrown

jobskill_ref.delete is not a function

like image 577
Amit Sinha Avatar asked Nov 08 '17 12:11

Amit Sinha


People also ask

How do you delete a document on firestore?

Call the doc() method and pass references of database, collection name and ID of a document that we want to delete. Assign it to a constant called docRef. Let's make a deleteDoc() query to perform deleting a document from a cities collection in Cloud Firestore.

Which of the following method is used to delete a field from a particular document firebase?

Which of the following method is used to delete a field from a particular document in firebase? Deleting Fields For deleting a specific field from a document, use the FieldValue. delete() method when we update a document.


2 Answers

You can only delete a document once you have a DocumentReference to it. To get that you must first execute the query, then loop over the QuerySnapshot and finally delete each DocumentSnapshot based on its ref.

var jobskill_query = db.collection('job_skills').where('job_id','==',post.job_id); jobskill_query.get().then(function(querySnapshot) {   querySnapshot.forEach(function(doc) {     doc.ref.delete();   }); }); 
like image 74
Frank van Puffelen Avatar answered Sep 25 '22 04:09

Frank van Puffelen


I use batched writes for this. For example:

var jobskill_ref = db.collection('job_skills').where('job_id','==',post.job_id); let batch = firestore.batch();  jobskill_ref   .get()   .then(snapshot => {     snapshot.docs.forEach(doc => {       batch.delete(doc.ref);     });     return batch.commit();   }) 

ES6 async/await:

const jobskills = await store   .collection('job_skills')   .where('job_id', '==', post.job_id)   .get();  const batch = store.batch();  jobskills.forEach(doc => {   batch.delete(doc.ref); });  await batch.commit(); 
like image 33
jamstooks Avatar answered Sep 25 '22 04:09

jamstooks