2016-12-02 29 views
0

我試圖計算每月的平均航班,我收到了一條錯誤在流水線階段MongoDB的錯誤對象

「流水線級規範對象必須只包含一個領域。」,

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"}}} 

]) 

回答

0

您的彙總管道有點不正確;特別是$ 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" 
     } 
    } 
    } 
]) 
相關問題