首页 > 解决方案 > 多个条件匹配时如何删除文档,firestore firebase

问题描述

当多个条件匹配时,我想删除一个 firestore 对象。像下面

db.collection('facilityOwners').where('facilityId', '==', facilityId).where('ownerId', '==', ownerId).delete()

在我的 facilityOwners 收藏中 在此处输入图像描述

当这两个属性匹配时,我想删除一个文档。而且我不想先找到文档然后执行删除。

标签: node.jsfirebasegoogle-cloud-firestore

解决方案


如果您知道该文档的完整、准确路径,则只能在 Firestore 中写入(并因此删除)该文档。Firestore 不支持等效的 SQLDELETE FROM facilityOwners WHERE ...查询。

这意味着您首先必须查看get()查询结果,遍历它们,然后单独或批量删除每个文档。就像是:

let query = db.collection('facilityOwners').where('facilityId', '==', facilityId).where('ownerId', '==', ownerId);
query.get().then((querySnapshot) => {
  querySnapshot.forEach((doc) => {
    doc.ref.delete();
  });
});

另见:


推荐阅读