我使用Mongodb Aggregation
框架'm trying to implement the following query as efficient as possible so I'm .
我有一个名为 mydevices
的集合,其中包含各自代表以下格式的设备的文档:
{
BUILDING: 'stringValue',
DEVICECLASS: 'stringValue',
}
我想要实现的输出是每个 BUILDING
的数组,其中包含每个类别中每个 DEVICECLASS
的设备数,例如
'building xy': {
'mobile phone': 5,
'destkop': 3
}
到目前为止我尝试了什么:
collection.aggregate(
[
{
$group:{ // group by building
_id: {BUILDING},
DEVCLASS: {DEVCLASS} // save devclass as property
}
}
]
)
在我的情况下,似乎 $count
运算符没有帮助?
$ count返回一个文档,其中包含输入到舞台的文档数量的计数 .
我已阅读文档,但现在我被卡住了 . 我怎样才能达到效果?
1 回答
尝试:
你会得到: