在Mongo中嵌套$ pull命令

时间:2017-07-17 16:27:22

标签: mongodb bson mongo-c-driver

我试图从多个数组中删除一个值,而不必发出多个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 个答案:

答案 0 :(得分: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), 
    "}"
    );