在 Mongo 中嵌套 $pull 命令

nesting $pull commands in Mongo

我试图从多个数组中删除一个值,而不必发出多个 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), 
        "}"
        );

$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), 
    "}"
    );