2017-07-17 151 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), 
    "}" 
    );