【发布时间】:2017-06-19 15:09:21
【问题描述】:
我需要这样的查询:
db.items.aggregate([
{
$group: {
_id: '$dataset_id',
labeledCount: {WHERE labels.length>0}, // this line is problem
totalCount: {$sum: 1}
}
}])
Item 对象如下所示:
{
_id: 1,
labels: [...]
...
}
结果应该是这样的:
{_id: 1, labeledCount: 4, totalCount: 5},
{_id: 2, labeledCount: 0, totalCount: 4},
...
编辑:
这是我最接近的:
db.items.aggregate(
[
{
$group : {
_id : "$dataset_id",
"labeled" : {
$sum : {
$cond : { if: { "$labels.0": { "$exists": true } }, then: 1, else: 0}
}
},
"total" : { $sum : 1 }
}
}
]
)
现在我收到错误:
"例外:虚线字段名只允许在顶层",
【问题讨论】:
标签: mongoose count group-by aggregate