2017-07-17 152 views
0

我試圖從多個數組中刪除一個值,而不必發出多個Mongo命令。我必須有我的語法不正確,任何幫助將不勝感激。

當我嘗試:

update = BCON_NEW("$pull", 
     "{", 
      "files.$.like", BCON_UTF8 (account_id), 
     "}", 
     "{", 
      "files.$.hate", BCON_UTF8 (account_id), 
     "}", 
     "{", 
      "files.$.love", BCON_UTF8 (account_id), 
     "}", 
     "{", 
      "files.$.funny", BCON_UTF8 (account_id), 
     "}", 
     "{", 
      "files.$.sad", BCON_UTF8 (account_id), 
     "}", 
     "{", 
      "files.$.anger", BCON_UTF8 (account_id), 
     "}", 
     "{", 
      "files.$.kiss", BCON_UTF8 (account_id), 
     "}" 
     ); 

,如果我把它簡化到只有它的工作原理如下失敗:

update = BCON_NEW("$pull", 
     "{", 
      "files.$.like", BCON_UTF8 (account_id), 
     "}" 
     ); 

回答

1

$pull在它後面的文件進行操作。見MongoDB Documentation on $pull。我從來沒有見過的BCON符號之前了,所以我可能是錯的,但如果我理解正確的話,我相信,你的代碼創建的文檔應該是這樣的:

{ 
    $pull: { "files.$.like": BCON_UTF8 (account_id) } //Here's the end of your $pull document, 
    { "files.$.hate": BCON_UTF8 (account_id) }, 
    { "files.$.love": BCON_UTF8 (account_id) }, 
    ... 
}, 

相反,我認爲你想看起來像這樣的東西(我還沒有測試過):

update = BCON_NEW("$pull", 
    "{", 
     "files.$.like", BCON_UTF8 (account_id), 
     "files.$.hate", BCON_UTF8 (account_id), 
     "files.$.love", BCON_UTF8 (account_id), 
     "files.$.funny", BCON_UTF8 (account_id), 
     "files.$.sad", BCON_UTF8 (account_id), 
     "files.$.anger", BCON_UTF8 (account_id), 
     "files.$.kiss", BCON_UTF8 (account_id), 
    "}" 
    );