【发布时间】:2015-06-11 17:07:03
【问题描述】:
我收藏了db.problems。每个文档都包含嵌套的 area 和 category 对象。文档示例:
{
_id: 1,
text: 'Hello',
area: {_id: 23, title: 'Area 61'},
category: {_id: 12, title: 'Just a category'}
}
我正在尝试按类别计算每个领域的问题并得到如下结果:
[
{
area: {_id: 2, title: 'Area 61'},
categoriesStats: {
12: {title: 'Just a category', problemCount: 123},
42: {title: 'Another category', problemCount: 11}
}
},
{...}
]
主要是categoriesStats必须是一个hash(以category的id为key)
我现在想到的:
db.problems.aggregate([
{$group: {
_id: {areaId: '$area._id', categoryId: '$category._id'},
problemCount: {$sum: 1},
area: {$first: '$area'},
category: {$first: '$category'}
}},
{$group: {
_id: '$_id.areaId',
area: {$first: '$area'},
categoriesStats: {
$push: {
problemCount: '$problemCount',
category: '$category'
}
}
}},
{$project: {_id: 0, area: 1, categoriesStats: 1}}
])
本次查询结果:
{
"result": [
{
"area": {"_id": 37, "name": "Some area"},
"categoriesStats": [
{
"problemCount": 1,
"category": {"_id": 4, "title": "Just a cat"}
},
{
"problemCount": 1,
"category": {"_id": 3, "title": "Misc"}
}
]
},
{
"area": {"_id": 36, "name": "wow such area"},
"categoriesStats": [
{
"problemCount": 1,
"category": {"_id": 4, "title": "Just a cat"}
},
{
"problemCount": 2,
"category": {"_id": 3, "title": "Misc"}
}
]
}
],
"ok": 1
}
如您所见,我设法获得了几乎需要的结果,但我无法将 categoriesStats 作为哈希。
我在$project 阶段尝试过查询,例如
{$project: {'$category._id': '$categories'},但是
"$project 的顶层不允许使用 $ 表达式"
我也尝试过这样预定义查询:
(3 是某个类别的_id)
{$group: ...},
{$project: {
'categoriesStats.3': {$cond: [{$eq: ['$category._id', 3]}, '$category', null]}},
//Same field for every category _id
{$group: ...}
但在这种情况下,我无法通过 $group 阶段获取此哈希
那么,问题是,有没有办法以 hashmap 形式获得categoriesStats?
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework