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

1 回答 1

1

$pull紧随其后的文档进行操作。请参阅$pull 上的 MongoDB 文档。我以前从未见过 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), 
    "}"
    );
于 2017-07-20T04:19:59.657 回答