字段未显示在Mongoose聚合中

时间:2012-10-08 12:49:00

标签: mongodb mongoose aggregation-framework

我正在试图统计所有不同的部门。我希望结果包含deptType,deptName和count。分组工作,结果显示deptType和dCount,但不显示deptName。知道为什么吗?

我的数据如下:

{
  "_id": "10280",
  "city": "NEW YORK",
  "state": "NY",
  "departments": [
         {"departmentType":"01",
          "departmentHead":"Peter"},
         {"departmentType":"02",
          "departmentHead":"John"}
  ]
},
{
  "_id": "10281",
  "city": "LOS ANGELES",
  "state": "CA",
  "departments": [
         {"departmentType":"02",
          "departmentHead":"Joan"},
         {"departmentType":"03",
          "departmentHead":"Mary"}
]
}

和我的Mongoose聚合命令如下:

Departments.aggregate(
{  
$project : {"departments.deptType" : 1,
        "departments.deptName" : 1,
        "dCount" : 1  } 
},
{ 
    $match: {"departments.deptType": {$exists: true}} 
},
{  
    $unwind: "$departments" 
},
{ 
    $group: {
        _id: "$departments.deptType",
         dCount: { $sum: 1 },
    }
},
{
$match: { dCount: { $gt: 5 }   }
},
{ $sort: { dCount: -1 } },
{ $limit : 50 },  

function(err, dbres) {}
);

提前谢谢大家。

1 个答案:

答案 0 :(得分:0)

对不起那些傻的家伙:

(...)
$group: {
    _id : { dType : "$departments.deptType", 
            dName : "$departments.deptName" 
          },
   dCount: { $sum: 1 },
}
(...)