Javascript 如何使用where子句从firestore中删除文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47180076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:46:16  来源:igfitidea点击:

How to delete document from firestore using where clause

javascriptfirebasegoogle-cloud-firestore

提问by Amit Sinha

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

jobskill_ref.delete 不是函数

回答by Frank van Puffelen

You can only delete a document once you have a DocumentReferenceto it. To get that you must first execute the query, then loop over the QuerySnapshotand finally delete each DocumentSnapshotbased on its ref.

您只能在拥有文档后才能删除DocumentReference它。要获得它,您必须首先执行查询,然后循环QuerySnapshot并最后DocumentSnapshot根据其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();
  });
});

回答by jamstooks

I use batched writesfor 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:

ES6 异步/等待:

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();

回答by Jacob Hixon

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

该固定我的问题,坦率的回答的关键部分是.refdoc.ref.delete()

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

我最初只有doc.delete()which 给出了“不是函数”错误。现在我的代码看起来像这样并且完美运行:

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);
});

回答by HMagdy

And of course, you can use await/async:

当然,您可以使用 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 loopon 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.

我不知道为什么你必须get()它们并循环它们,然后delete()它们,而你可以像任何 SQL 语句一样准备一个查询,在一个步骤中删除哪里,但谷歌决定这样做。所以,就目前而言,这是唯一的选择。

回答by Luis Figueredo

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');
}