MongoDB-投射一个字段表现得很奇怪

时间:2018-03-17 06:06:43

标签: mongodb mongodb-query aggregation-framework

除非我按以下查询中的说明添加title两次,否则我无法投射title: 1。任何人都可以解释为什么以及如何解决它?

db.movies.aggregate([

{$match: {
   languages: {$elemMatch : {$eq: "English"}},
   "imdb.rating":{$gte: 1},
   "imdb.votes":{$gte: 1},
   year: {$gte: 1990}
}},
{$project:
   {scaled_votes: {$add: [1, {$multiply: [9, {$divide: [{$subtract:
     ["$imdb.votes",5]},{$subtract: [1521105,5]}]}]}]},
      "imdb.rating": 1,
       title: 1
    }

  },
  {$project: 
    {           
        normalized_rating: {$avg:["$scaled_votes","$imdb.rating"]},title: 1
    }
   },
   {$sort: {normalized_rating: 1}},
   {$limit: 1}


  ])

示例数据:

/* 1 */
{
 "_id" : ObjectId("573a1398f29313caabcebce8"),
"title" : "12:01 PM",
"year" : 1990,
"runtime" : 25,
"cast" : [ 
    "Jane Alden", 
    "Don Amendolia", 
    "John Bachelder", 
    "Rick Ford"
],
"plot" : "A man is stuck living his life in the same 59 minute time frame. He tries various methods of finding out why, eventually consulting a physicist.",
"fullplot" : "A man is stuck living his life in the same 59 minute time frame. He tries various methods of finding out why, eventually consulting a physicist.",
"lastupdated" : "2015-06-04 00:41:47.527000000",
"type" : "movie",
"languages" : [ 
    "English"
],
"directors" : [ 
    "Jonathan Heap"
],
"writers" : [ 
    "Stephen Tolkin", 
    "Jonathan Heap", 
    "Richard Lupoff (short story)"
],
"imdb" : {
    "rating" : 7.8,
    "votes" : 856,
    "id" : 98962
},
"countries" : [ 
    "USA"
],
"genres" : [ 
    "Sci-Fi", 
    "Short"
],
"tomatoes" : {
    "viewer" : {
        "rating" : 0.0,
        "numReviews" : 0
    },
    "lastUpdated" : ISODate("2015-09-14T18:07:54.000Z")
},
"num_mflix_comments" : 2,
"comments" : [ 
    {
        "name" : "Daenerys Targaryen",
        "email" : "emilia_clarke@gameofthron.es",
        "movie_id" : ObjectId("573a1398f29313caabcebce8"),
        "text" : "Quasi praesentium libero sapiente quae. Maiores cupiditate laboriosam porro quas cupiditate fugiat. Ipsum sunt in natus atque. Doloribus numquam recusandae harum repudiandae eos assumenda.",
        "date" : ISODate("2004-02-23T07:08:24.000Z")
    }, 
    {
        "name" : "Robert Smith",
        "email" : "robert_smith@fakegmail.com",
        "movie_id" : ObjectId("573a1398f29313caabcebce8"),
        "text" : "Nesciunt magnam doloremque deserunt. Itaque culpa a ad optio sint impedit mollitia provident. Impedit aut assumenda ab cupiditate repudiandae dolore.",
        "date" : ISODate("1970-09-03T23:22:31.000Z")
    }
]
}

1 个答案:

答案 0 :(得分:1)

$ project运算符要求您列出新字段和现有字段。

用以下项目阶段替换您的两个项目阶段。

{
  $project: {
    normalized_rating: {
      $avg: [
        {
          $add: [
            1,
            {
              $multiply: [
                9,
                {
                  $divide: [
                    {
                      $subtract: [
                        "$imdb.votes",
                        5
                      ]
                    },
                    {
                      $subtract: [
                        1521105,
                        5
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        },
        "$imdb.rating"
      ]
    },
    title: 1
  }
}