How to delete document from firestore using where

2020-01-29 07:03发布

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

5条回答
闹够了就滚
2楼-- · 2020-01-29 07:13

the key part of Frank's answer that fixed my issues was the .ref in doc.ref.delete()

I originally only had doc.delete() which gave a "not a function" error. now my code looks like this and works perfectly:

let fs = firebase.firestore();
let collectionRef = fs.collection(<your collection here>);

collectionRef.where("name", "==", name)
.get()
.then(querySnapshot => {
  querySnapshot.forEach((doc) => {
    doc.ref.delete().then(() => {
      console.log("Document successfully deleted!");
    }).catch(function(error) {
      console.error("Error removing document: ", error);
    });
  });
})
.catch(function(error) {
  console.log("Error getting documents: ", error);
});
查看更多
Juvenile、少年°
3楼-- · 2020-01-29 07:13

And of course, you can use await/async:

exports.delete = functions.https.onRequest(async (req, res) => {
try {
    var jobskill_ref = db.collection('job_skills').where('job_id','==',post.job_id).get();
    jobskill_ref.forEach((doc) => {
      doc.ref.delete();
    });
  } catch (error) {
    return res.json({
      status: 'error', msg: 'Error while deleting', data: error,
    });
  }
});

I have no idea why you have to get() them and loop on them, then delete() them, while you can prepare one query with where to delete in one step like any SQL statement, but Google decided to do it like that. so, for now, this is the only option.

查看更多
forever°为你锁心
4楼-- · 2020-01-29 07:23

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();
  });
});
查看更多
三岁会撩人
5楼-- · 2020-01-29 07:23

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();
查看更多
beautiful°
6楼-- · 2020-01-29 07:31
delete(seccion: string, subseccion: string) 
{
 const deletlist = this.db.collection('seccionesclass', ref => ref.where('seccion', '==', seccion).where('subseccion', '==' , subseccion))
deletlist.get().subscribe(delitems => delitems.forEach( doc=> doc.ref.delete()));
    alert('record erased');
}
查看更多
登录 后发表回答