你好我是mongodb的新手,并尝试将不同类型的对象(int)转换为键值对。MongoDB聚合组数组键值:总和值
我有收集这样的:
{
"_id" : ObjectId("5372a9fc0079285635db14d8"),
"type" : 1,
"stat" : "foobar"
},
{
"_id" : ObjectId("5372aa000079285635db14d9"),
"type" : 1,
"stat" : "foobar"
},
{
"_id" : ObjectId("5372aa010079285635db14da"),
"type" : 2,
"stat" : "foobar"
},{
"_id" : ObjectId("5372aa030079285635db14db"),
"type" : 3,
"stat" : "foobar"
}
我想结果是这样的:
{
"type1" : 2, "type2" : 1, "type3" : 1,
"stat" : "foobar"
}
目前正在汇聚组,然后按类型值数组
db.types.aggregate(
{$group : {
_id : "$stat",
types : {$push : "$type"}
}}
)
但不知道如何总结不同的类型s并将其转换为关键值
/* 0 */
{
"result" : [
{
"_id" : "foobar",
"types" : [
1,
2,
2,
3
]
}
],
"ok" : 1
}
:// JIRA .mongodb.org/browse/SERVER-5947,因为目前不可能。 –
嗨,Asya,我投了这张杰拉票。 –