【问题标题】:Aggregate distinct values in MongoDB在 MongoDB 中聚合不同的值
【发布时间】:2018-03-18 17:18:26
【问题描述】:

我有一个包含 18625 个集合的 mongodb 数据库。它有以下键:

    "_id" : ObjectId("5aab14d2fc08b46adb79d99c"), 
    "game_id" : NumberInt(4), 
    "score_phrase" : "Great", 
    "title" : "NHL 13", 
    "url" : "/games/nhl-13/ps3-128181", 
    "platform" : "PlayStation 3", 
    "score" : 8.5, 
    "genre" : "Sports", 
    "editors_choice" : "N", 
    "release_year" : NumberInt(2012), 
    "release_month" : NumberInt(9), 
    "release_day" : NumberInt(11)

现在,我希望创建另一个只有流派的维度/集合。

如果我使用以下查询:

db.ign.aggregate([ {$project: {"genre":1}}, { $out: "dimen_genre" } ]);

它生成 18625 个集合,即使只有 113 个不同的集合 流派。

如何在此处应用 distinct 并获取仅具有不同 113 值的流派的集合。 我用谷歌搜索,它表明聚合和不同在 mongo 中不能一起工作。 我也试过:db.dimen_genre.distinct('genre').length 这表明在 dimension_genre 中有 113 个不同的流派。

确切地说, 如何从现有的集合中创建一个只有不同值的集合。

我对 NoSQL 真的很陌生。

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您可以使用$addToSet 将唯一值分组到一个文档中,然后使用$unwind 获取多个文档:

    db.ign.aggregate([
        {
            $group: {
                _id: null,
                genre: { $addToSet: "$genre" }
            }
        },
        {
            $unwind: "$genre"
        },
        {
            $project: {
                _id: 0
            }
        },
        { $out: "dimen_genre" }
    ]);
    

    【讨论】:

    • 谢谢!!它有效,如何使用它创建一个新集合,这样我就不需要一次又一次地调用这个查询
    • @NidhiGarg 在您的示例中使用 $out
    • 非常感谢,它也帮助我理解了许多其他查询背后的逻辑。
    • 您能解释一下如何在新集合中添加多个键,例如同时采用流派和发布年份吗?
    • @NidhiGarg 您必须在$addToSet 之前指定字段名称,例如{ $group: { _id: null, fieldName: {$addToSet: { release_year: "$release_year",release_month: "$release_month",release_day: "$release_day" } } } }
    【解决方案2】:

    你可以试试

    db.names.aggregate(
       [
         { $group : { _id : "$genre", books: { $push: "$$ROOT" } } }
       ]
    )
    

    我尝试过将测试和体育作为流派

    它为您提供类似这样的输出

    {
        "_id" : "Test",
        "books" : [ 
            {
                "_id" : ObjectId("5aaea6150cc1403ee9a02e0c"),
                "game_id" : 4,
                "score_phrase" : "Great",
                "title" : "NHL 13",
                "url" : "/games/nhl-13/ps3-128181",
                "platform" : "PlayStation 3",
                "score" : 8.5,
                "genre" : "Test",
                "editors_choice" : "N",
                "release_year" : 2012,
                "release_month" : 9,
                "release_day" : 11
            }
        ]
    }
    
    /* 2 */
    {
        "_id" : "Sports",
        "books" : [ 
            {
                "_id" : ObjectId("5aaea3be0cc1403ee9a02d97"),
                "game_id" : 4,
                "score_phrase" : "Great",
                "title" : "NHL 13",
                "url" : "/games/nhl-13/ps3-128181",
                "platform" : "PlayStation 3",
                "score" : 8.5,
                "genre" : "Sports",
                "editors_choice" : "N",
                "release_year" : 2012,
                "release_month" : 9,
                "release_day" : 11
            }, 
            {
                "_id" : ObjectId("5aaea3c80cc1403ee9a02d9b"),
                "game_id" : 4,
                "score_phrase" : "Great",
                "title" : "NHL 13",
                "url" : "/games/nhl-13/ps3-128181",
                "platform" : "PlayStation 3",
                "score" : 8.5,
                "genre" : "Sports",
                "editors_choice" : "N",
                "release_year" : 2012,
                "release_month" : 9,
                "release_day" : 11
            }, 
            {
                "_id" : ObjectId("5aaea3cf0cc1403ee9a02d9f"),
                "game_id" : 4,
                "score_phrase" : "Great",
                "title" : "NHL 13",
                "url" : "/games/nhl-13/ps3-128181",
                "platform" : "PlayStation 3",
                "score" : 8.5,
                "genre" : "Sports",
                "editors_choice" : "N",
                "release_year" : 2012,
                "release_month" : 9,
                "release_day" : 11
            }
        ]
    }
    

    【讨论】:

    • 感谢您的时间和解决方案,但我想我此时并没有完全在寻找这个 :)
    猜你喜欢
    • 1970-01-01
    • 2018-05-25
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多