2017-02-10 63 views
0

隨着火力地堡扇出數據到不同的節點和路徑由火力地堡像例如由火力地堡樣品低於推薦:如何在Firebase數據庫中以扇出方式存在多個路徑時進行一致性刪除?

{ 
    "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" : "[email protected]", 
     "username" : "user1" 
    } 
    } 
} 
  1. 隨着多路徑的更新,我們可以以原子方式更新信息中的所有的路徑,但是,如果我們想要在上述類型的模式中刪除博客文章,那麼我們如何以原子方式執行它?我想,沒有多路徑刪除。如果客戶端在刪除時會丟失網絡連接,則只會刪除幾條路徑!

  2. 此外,如果有一個要求當用戶被刪除的所有他已經加星標的帖子,我們應該刪除星星和unstar該用戶的帖子。這變得困難,因爲沒有直接跟蹤用戶已經出演的帖子。爲此,我們需要將帖子的主角扇出,就像有一個節點用戶明星一樣。然後,在刪除時,我們知道用戶在刪除用戶時所做的所有活動並對其執行操作。有沒有更好的方法來處理這個問題?

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

在這兩種情況下的原子或持續從多徑刪除數據的問題(無論是全有或全無)似乎是不可用的。

在這種情況下,唯一可用的選項似乎是將刪除命令放入Firebase隊列中,只有刪除了所有內容時,才能解析隊列中的任務。這將是最終一致的選擇,但應該沒問題。但這是昂貴的選項,需要服務器。有沒有更好的辦法?

+0

請每個帖子堅持一個問題。 –

回答

4

通過向路徑寫入值null,可以實現多路徑刪除。

所以:

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

我已經回答了這個前:Firebase -- Bulk delete child nodes

它也很明確the documentation on deleting data提到:

您也可以通過指定NULL作爲值刪除另一個寫操作,如set()或update()。您可以使用update()方法在單個API調用中刪除多個子項。

+0

謝謝。我搜索使用多路徑,使用多位置搜索字或批量我會得到它。雖然文件提到我認爲它會使數據爲空而不刪除密鑰(PostId)。 –

+0

我在文檔中添加了一個鏈接和引用。 –

+0

我相信你在更新數組@FrankvanPuffelen的最後一行缺少一個「:」。 –

相關問題