从字段中找到最高价值

时间:2019-03-07 01:42:34

标签: mongodb

我在mongodb中有以下对象。我想找到获得最多奖项的类型,并在整个收藏中找到最常发现的前3种类型。我不太确定如何针对此类集合中的特定字段,将其视为大型数组会更好吗?或者这是一个荒谬的评论。

尝试的查询由于类型字段不是累加器而失败

db.MovieData.aggregate([
{$sort:{"awards.wins":-1}},
{$group:{"genres":"$genres"}}
])

示例数据,数据更多,但我仅限于2次插入

[
    {
        "title": "Once Upon a Time in the West",
        "year": 1968,
        "rated": "PG-13",
        "runtime": 175,
        "countries": [
            "Italy",
            "USA",
            "Spain"
        ],
        "genres": [
            "Western"
        ],
        "director": "Sergio Leone",
        "writers": [
            "Sergio Donati",
            "Sergio Leone",
            "Dario Argento",
            "Bernardo Bertolucci",
            "Sergio Leone"
        ],
        "actors": [
            "Claudia Cardinale",
            "Henry Fonda",
            "Jason Robards",
            "Charles Bronson"
        ],
        "plot": "Epic story of a mysterious stranger with a harmonica who joins forces with a notorious desperado to protect a beautiful widow from a ruthless assassin working for the railroad.",
        "poster": "http://ia.media-imdb.com/images/M/MV5BMTEyODQzNDkzNjVeQTJeQWpwZ15BbWU4MDgyODk1NDEx._V1_SX300.jpg",
        "imdb": {
            "id": "tt0064116",
            "rating": 8.6,
            "votes": 201283
        },
        "tomato": {
            "meter": 98,
            "image": "certified",
            "rating": 9,
            "reviews": 54,
            "fresh": 53,
            "consensus": "A landmark Sergio Leone spaghetti western masterpiece featuring a classic Morricone score.",
            "userMeter": 95,
            "userRating": 4.3,
            "userReviews": 64006
        },
        "metacritic": 80,
        "awards": {
            "wins": 4,
            "nominations": 5,
            "text": "4 wins \u0026 5 nominations."
        },
        "type": "movie"
    },
    {
        "title": "A Million Ways to Die in the West",
        "year": 2014,
        "rated": "R",
        "runtime": 116,
        "countries": [
            "USA"
        ],
        "genres": [
            "Comedy",
            "Western"
        ],
        "director": "Seth MacFarlane",
        "writers": [
            "Seth MacFarlane",
            "Alec Sulkin",
            "Wellesley Wild"
        ],
        "actors": [
            "Seth MacFarlane",
            "Charlize Theron",
            "Amanda Seyfried",
            "Liam Neeson"
        ],
        "plot": "As a cowardly farmer begins to fall for the mysterious new woman in town, he must put his new-found courage to the test when her husband, a notorious gun-slinger, announces his arrival.",
        "poster": "http://ia.media-imdb.com/images/M/MV5BMTQ0NDcyNjg0MV5BMl5BanBnXkFtZTgwMzk4NTA4MTE@._V1_SX300.jpg",
        "imdb": {
            "id": "tt2557490",
            "rating": 6.1,
            "votes": 126592
        },
        "tomato": {
            "meter": 33,
            "image": "rotten",
            "rating": 4.9,
            "reviews": 188,
            "fresh": 62,
            "consensus": "While it offers a few laughs and boasts a talented cast, Seth MacFarlane's overlong, aimless A Million Ways to Die in the West is a disappointingly scattershot affair.",
            "userMeter": 40,
            "userRating": 3,
            "userReviews": 62945
        },
        "metacritic": 44,
        "awards": {
            "wins": 0,
            "nominations": 6,
            "text": "6 nominations."
        },
        "type": "movie"
    }

1 个答案:

答案 0 :(得分:2)

您正在寻找的是:

db.MovieData.aggregate([
  { "$unwind": "$genres" },
  { "$group": {
    "_id": "$genres",
    "totalWins": { "$sum": "$awards.wins" }
  }},
  { "$sort": { "totalWins": -1 } },
  { "$limit": 3 }
])

简而言之:

  • $unwind-genres字段是一个数组,您需要将该“ flattened” 用作“分组键” < / em>用于下一个阶段:

  • $group-需要一个_id,它是“分组密钥” 为其累加的值。尽管不是需求,它通常与累加器配对,累加器在提供的字段上执行“聚合操作” ,例如$sum值。您想在这里

    { "$sum": "$awards.wins" }
    

    累积该字段。

  • $sort-通过提供的字段对这些结果进行排序。在这种情况下,按累积的totalWins并按降序-1)的顺序进行。
  • $limit-要返回的结果文档数 limit

核心文档中的SQL to Aggregation Mapping Chart是查找常见示例的好地方,特别是如果您对SQL有一定的了解,或者即使您不是一般示例。

所有Aggregation Pipeline StagesAggregation Pipeline Operators在其自己的文档页面中也都有各种用法示例。熟悉这些内容有助于理解它们如何应用于不同的问题