根据 criteria/child 在 Firebase 数据库管理员中的值删除所有数据的好方法?

Good way to delete all data according to criteria/child's value in Firebase database admin?

我想通过删除其具有 isTesting == true 的所有子节点来清理此 userPublic。我正在使用 Firebase 的云功能。我的方法是:

const userPublic = admin.database().ref("/userPublic")
const testsInUserPublic = userPublic.orderByChild("isTesting").equalTo(true)
testsInUserPublic.once("value", dataSnapshot => {
    // ???
})
  1. 因为我只能在引用上调用.remove()而不能调用快照但是为了过滤我想要它的子returns快照,我如何从快照中获取引用? (我想知道每个过滤子项的密钥 XXX-XXX-XXX,所以我可以用 userPublic.remove() 将它们一一连接起来)

  2. 此外,即使我可以获得我想要删除的所有引用,我认为通过调用 .remove() 一个一个地删除它们,然后等待承诺,然后调用下一个听起来不是最佳方式。有什么办法可以一次性全部删除吗?

  3. 如果它涉及在顶部 userPublic 节点上调用 .update(),我将不得不获取所有内容,删除带有 isTesting 的那个,然后将剩余的返回更新。与过滤方式相比,这听起来效率不高。因为最终带有 .isTesting 的数据只占所有数据的 5% 左右。或者这实际上是每个人都在使用的方法吗?

你快到了。剩下的就是根据查询结果创建单个多位置更新:

const userPublic = admin.database().ref("/userPublic")
const testsInUserPublic = userPublic.orderByChild("isTesting").equalTo(true)
testsInUserPublic.once("value", snapshot => {
    var updates = {};
    snapshot.forEach(function(child) {
      updates["/userPublic/"+child.key] = null;
    });
    userPublic.update(updates);
})

用 promise 做这件事不会有太大的不同:

testsInUserPublic.once("value", snapshot => {
    var promises = [];
    snapshot.forEach(function(child) {
      promises.push(child.ref.remove());
    });
    Promise.all(promises); // this returns a promise that resolves once all deletes are done, or that rejects once one of them fails
})

其性能将非常相似,因为 Firebase 通过单个连接对请求进行流水线处理。参见