【发布时间】:2014-05-13 23:37:41
【问题描述】:
您好,我是 mongodb 新手,正在尝试将不同类型 (int) 的对象转换为键值对。
我有这样的收藏:
{
"_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"}
}}
)
但不知道如何对不同的类型求和并转换成键值
/* 0 */
{
"result" : [
{
"_id" : "foobar",
"types" : [
1,
2,
2,
3
]
}
],
"ok" : 1
}
【问题讨论】:
-
如果您希望能够将值映射到键,请投票给这张 jira 票 jira.mongodb.org/browse/SERVER-5947,因为目前还不可能。
-
嗨 Asya,我为这张 jira 票投了票。
标签: mongodb mapreduce aggregation-framework