0

我正在尝试计算所有不同部门的数量。我希望结果包含 deptType、deptName 和计数。分组有效,结果显示 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"}
]
}

和我的猫鼬聚合命令是这样的:

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) {}
);

谢谢大家。

4

1 回答 1

0

对不起,愚蠢的家伙:

(...)
$group: {
    _id : { dType : "$departments.deptType", 
            dName : "$departments.deptName" 
          },
   dCount: { $sum: 1 },
}
(...)
于 2012-10-08T13:08:18.337 回答