Angularfire2 + Firestore查询结果与引用

时间:2018-01-28 21:47:17

标签: angularfire2 google-cloud-firestore

我在一个集合中有几百个文档。其中很少被破坏,我想删除它们。我知道如何找到损坏的文档,我得到它们作为查询的结果。但它只是一个数据,没有文档ID或任何东西。

所以我的问题是,如何删除我在查询中收到的文件?或者是否有其他方法可以根据某些属性删除文件?

getData(target) {
    return this.afs.collection('someCollection', ref => {
      let query: firebase.firestore.CollectionReference | firebase.firestore.Query = ref;
      query = query.where('label', '==', target);
      return query;
    });
  }

this.dataService.getData('CorruptedLabel').valueChanges().subscribe(resp => {
      console.log('resp', resp); // Here I get and array of objects
      // I would like to go through that array and call delete() on each item
    });

1 个答案:

答案 0 :(得分:1)

您可以获取firestore文档参考并删除文档

this.dataService.getData('CorruptedLabel').snapshotChanges().subscribe(snapshots
=> {
  snapshots.forEach(snapshot => {
     if(snapshot){
          this.afs.collection('someCollection').doc(snapshot.payload.doc.id).delete();
      }
  }
});