Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB投射场的行为很奇怪_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

MongoDB投射场的行为很奇怪

MongoDB投射场的行为很奇怪,mongodb,mongodb-query,aggregation-framework,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:

我无法投影
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")
    }
]
}

$project operator要求您列出新字段和现有字段

将两个项目阶段替换为以下项目阶段

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

我设法绕过并删除了一次事件的
限制:1
。然而,我仍然想知道如何在不考虑
$addFields
的情况下修复上述查询。感谢最短的解决方案,我只是想知道如果我一次(在上述查询的管道中的第一个或最后一个)包含
title:1
,为什么
title
没有得到投影。这就是聚合的工作原理。聚合的工作原理是将数据从一个阶段传递到另一个阶段。匹配和排序阶段对数据应用筛选和排序,而项目和组阶段通过列出下一阶段所需的字段来转换数据。在下一阶段中,您将使用上一阶段中的字段,并定义要为下一阶段保留的字段,依此类推。另一方面,addFields通过在定义新字段时保留现有字段来简化操作。我的回答/评论对您有帮助吗?让我知道。