【问题标题】:nesting $pull commands in Mongo在 Mongo 中嵌套 $pull 命令
【发布时间】:2017-07-20 12:19:59
【问题描述】:

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

【问题讨论】:

    标签: mongodb bson mongo-c-driver


    【解决方案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), 
        "}"
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-21
      • 2014-04-05
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      相关资源
      最近更新 更多