【问题标题】:MongoDB error objects in pipeline stage管道阶段的 MongoDB 错误对象
【发布时间】:2016-12-02 00:35:43
【问题描述】:

我正在尝试计算每月的平均航班,但我收到一个错误

"一个流水线阶段规范对象必须只包含一个字段。",

db.Flights.aggregate([
{$unwind: "$flights"},
{$project: 
    {_id: 0,
    status: 1,
    flights: 1
},

$match: {"status": "active"},
$group: {_id: {"flights" : "$flights.flight_id", "Month":       "$depart_info.month_name_long"}, 
avg_flights: {$avg: "$flights.count"}}}

])

【问题讨论】:

    标签: mongodb match aggregation-framework


    【解决方案1】:

    您的聚合管道格式有些不正确;特别是 $match 和 $group 阶段。每个阶段都需要是一个 JSON 文档。请尝试以下操作:

    db.Flights.aggregate([
      {
          $unwind: "$flights"
      }, 
      {
        $project: {
          _id: 0,
          status: 1,
          flights: 1
        },
      },
      {
        $match: {
          "status": "active"
        }
      },
      {
        $group: {
          _id: {
              "flights": "$flights.flight_id",
              "Month": "$depart_info.month_name_long"
          },
          avg_flights: {
              $avg: "$flights.count"
          }
        }
      }
    ])
    

    【讨论】:

      猜你喜欢
      • 2017-07-03
      • 1970-01-01
      • 2021-07-14
      • 2020-05-29
      • 2020-02-04
      • 2017-09-20
      • 2019-09-01
      • 2013-12-08
      • 2019-03-06
      相关资源
      最近更新 更多