当数据以扇出方式位于多个路径中时,如何在Firebase数据库中进行一致删除? [英] How to make consistent delete in Firebase database when the data lies in multiple paths in a fan out way?

查看:71
本文介绍了当数据以扇出方式位于多个路径中时,如何在Firebase数据库中进行一致删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase建议使用Firebase将数据扇出数据到不同的节点和路径,如以下Firebase示例中的示例所示:

With Firebase fan out data to different nodes and paths is recommended by Firebase like below example from Firebase sample:

{
  "post-comments" : {
    "PostId1" : {
      "CommentID1" : {
        "author" : "User1",
        "text" : "Comment1!",
        "uid" : "UserId1"
      }
    }
  },
  "posts" : {
    "PostId1" : {
      "author" : "user1",
      "body" : "Firebase Mobile platform",
      "starCount" : 1,
      "stars" : {
        "UserId1" : true
      },
      "title" : "About firebase",
      "uid" : "UserId1"
    }

  },
  "user-posts" : {
    "UserId1" : {
      "PostId1" : {
        "author" : "user1",
        "body" : "Firebase Mobile platform",
        "starCount" : 1,
        "stars" : {
          "UserId1" : true
        },
        "title" : "About firebase",
        "uid" : "UserId1"
      }

    }
  },
  "users" : {
    "UserId1" : {
      "email" : "user1@gmail.com",
      "username" : "user1"
    }
  }
}

  1. 通过多路径更新,我们可以原子地更新帖子的所有路径,但是,如果我们要删除上述类型的架构中的博客文章,那么我们该如何原子操作呢?我猜没有多路径删除.如果客户端在删除时失去网络连接,则仅删除很少的路径!

  1. With multipath updates we can atomically update all the paths for a post, however if we want to delete a blog post in above kind of schema then how can we do it atomically? There is no multi path delete, I guess. If client losses network connection while deleting then only few paths would be deleted!

此外,如果删除某位用户已加注星标的帖子时有要求 like ,我们应该删除该星标并取消对该用户的加注星标.由于无法直接跟踪用户已加注星标的位置,因此这变得很困难.为此,我们需要散布帖子的星标,以及拥有节点用户星标.然后,在删除用户时,我们知道用户执行的所有活动,并在删除用户时对其进行操作.有没有更好的方法来解决这个问题?

Also in case there is a requirement like when a user is deleted for all the post he has starred, we should remove the stars and unstar the post for that user. This becomes difficult as there is no direct tracking of what posts user has starred. For this do we need to fan out the starring of posts as well like have a node user-stars. Then while deleting we know what all activity the user has done and act on it while deleting user. Is there a better way of handling this?

"user-stars":{
    "UserId1":{
        "PostID1":true  
    }
}

在这两种情况下,似乎都无法自动或连续地从多路径(全部或全部)删除数据.

In both cases the question on atomically or consistently deleting the data from multipaths (either all or nothing) is seemingly not available.

在这种情况下,唯一可用的选项似乎是将delete命令放入Firebase队列中,这仅在删除所有内容后才能将任务解析到队列中.那将是最终一致的选择,但应该没问题.但这是需要服务器的昂贵选择.有更好的方法吗?

In that case the only option available looks to be putting the delete command in Firebase queue which will resolve the task in queue only if everything is deleted. That will be eventually consistent option but should be fine. But that is expensive option requiring server. Is there a better way?

推荐答案

您可以通过将null值写入路径来实现多路径删除.

You can implement a multi-path delete, by writing a value of null to the paths.

所以:

var updates = {
  "user-posts/UserId1/PostId1": null,
  "post-comments/PostId1": null,
  "posts/PostId1": null
}
ref.update(updates);

我之前已经回答过: Firebase-批量删除子节点

有关删除数据的文档中也已明确提及:

您还可以通过将null指定为另一个写操作(例如set()或update())的值来删除.您可以将此技术与update()结合使用,以在单个API调用中删除多个子级.

You can also delete by specifying null as the value for another write operation such as set() or update(). You can use this technique with update() to delete multiple children in a single API call.

这篇关于当数据以扇出方式位于多个路径中时,如何在Firebase数据库中进行一致删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆