MongoDB聚合组数组到键:和值

MongoDB聚合组数组到键:和值,mongodb,mapreduce,aggregation-framework,Mongodb,Mapreduce,Aggregation Framework,您好,我是mongodb新手,正在尝试将不同类型(int)的对象转换为键值对 我有这样的收藏: { “_id”:ObjectId(“5372a9fc0079285635db14d8”), “类型”:1, “stat”:“foobar” }, { “_id”:ObjectId(“5372aa000079285635db14d9”), “类型”:1, “stat”:“foobar” }, { “_id”:ObjectId(“5372aa010079285635db14da”), “类型”:2, “s

您好,我是mongodb新手,正在尝试将不同类型(int)的对象转换为键值对

我有这样的收藏:

{
“_id”:ObjectId(“5372a9fc0079285635db14d8”),
“类型”:1,
“stat”:“foobar”
},
{
“_id”:ObjectId(“5372aa000079285635db14d9”),
“类型”:1,
“stat”:“foobar”
},
{
“_id”:ObjectId(“5372aa010079285635db14da”),
“类型”:2,
“stat”:“foobar”
},{
“_id”:ObjectId(“5372aa030079285635db14db”),
“类型”:3,
“stat”:“foobar”
}
我想得到这样的结果:

{
“类型1”:2,“类型2”:1,“类型3”:1,
“stat”:“foobar”
}
当前正在尝试聚合组,然后将类型值推送到数组

db.types.aggregate(
{$group:{
_id:“$stat”,
类型:{$push:$type}
}}
)
但不知道如何对不同的类型求和并将其转换为键值

/*0*/
{
“结果”:[
{
“_id”:“foobar”,
“类型”:[
1.
2.
2.
3.
]
}
],
“好”:1
}

这离你够近吗

{ "_id" : "foobar", "types" : [ { "type" : "type3", "total" : 1 }, { "type" : "type2", "total" : 1 }, { "type" : "type1", "total" : 2 } ] }
这些类型位于一个数组中,但它似乎可以为您获取所需的数据。代码是:

db.types.aggregate(
    [{$group : {
        _id : "$stat",
        types : {$push : "$type"}
    }},
    {$unwind:"$types"},
    {$group: {
        _id:{stat:"$_id",
        types: {$substr: ["$types", 0, 1]}},
        total:{$sum:1}}},
    {$project: {
        _id:0,
        stat:"$_id.stat",
        type: { $concat: [ "type", "$_id.types" ] },
        total:"$total" }},
    {$group: {
        _id: "$stat",
        types: { $push: { type: "$type", total: "$total" } } }}
   ]
)

这离你够近吗

{ "_id" : "foobar", "types" : [ { "type" : "type3", "total" : 1 }, { "type" : "type2", "total" : 1 }, { "type" : "type1", "total" : 2 } ] }
这些类型位于一个数组中,但它似乎可以为您获取所需的数据。代码是:

db.types.aggregate(
    [{$group : {
        _id : "$stat",
        types : {$push : "$type"}
    }},
    {$unwind:"$types"},
    {$group: {
        _id:{stat:"$_id",
        types: {$substr: ["$types", 0, 1]}},
        total:{$sum:1}}},
    {$project: {
        _id:0,
        stat:"$_id.stat",
        type: { $concat: [ "type", "$_id.types" ] },
        total:"$total" }},
    {$group: {
        _id: "$stat",
        types: { $push: { type: "$type", total: "$total" } } }}
   ]
)

对于您的实际表单,因此假设您确实知道“type”的可能值,那么您可以通过两个阶段和一些运算符的使用来实现这一点:

db.types.aggregate([
{“$组”:{
“_id”:{
“统计”:“$stat”,
“类型”:“$type”
},
“计数”:{“$sum”:1}
}},
{“$组”:{
“\u id”:“$\u id.stat”,
“类型1”:{“$sum”:{“$cond”:[
{“$eq”:[“$\u id.type”,1]},
“$count”,
0
]}},
“类型2”:{“$sum”:{“$cond”:[
{“$eq”:[“$\u id.type”,2]},
“$count”,
0
]}},
“类型3”:{“$sum”:{“$cond”:[
{“$eq”:[“$\u id.type”,3]},
“$count”,
0
]}}
}}
])
这正好给出:

{u id:“foobar”,“type1”:2,“type2”:1,“type3”:1}
实际上,我更喜欢有两个阶段的动态形式:

db.types.aggregate([
{“$组”:{
“_id”:{
“统计”:“$stat”,
“类型”:“$type”
},
“计数”:{“$sum”:1}
}},
{“$组”:{
“\u id”:“$\u id.stat”,
“类型”:{“$push”:{
“类型”:“$\u id.type”,
“计数”:“$count”
}}
}}
])
输出不相同,但功能强大,可灵活调整值:

{
“_id”:“foobar”,
“类型”:[
{
“类型”:3,
“计数”:1
},
{
“类型”:2,
“计数”:1
},
{
“类型”:1,
“计数”:2
}
]
}
否则,如果您需要相同的输出格式,但需要灵活的字段,则始终可以使用mapReduce,但输出并不完全相同

db.types.mapReduce(
函数(){
var obj={};
var key=“type”+this.type;
obj[key]=1;
emit(this.stat,obj);
},
功能(键、值){
var obj={};
values.forEach(函数(值){
for(var k值){
if(!obj.hasOwnProperty(k))
obj[k]=0;
obj[k]++;
}
});
返回obj;
},
{“out”:{“inline”:1}
)
在典型的mapReduce样式中:

“结果”:[
{
“_id”:“foobar”,
“价值”:{
“类型1”:2,
“类型2”:1,
“类型3”:1
}
}
],

但是这些是您实际表单的选项,因此假设您确实知道“type”的可能值,那么您可以通过两个阶段和一些运算符的使用来完成此操作:

db.types.aggregate([
{“$组”:{
“_id”:{
“统计”:“$stat”,
“类型”:“$type”
},
“计数”:{“$sum”:1}
}},
{“$组”:{
“\u id”:“$\u id.stat”,
“类型1”:{“$sum”:{“$cond”:[
{“$eq”:[“$\u id.type”,1]},
“$count”,
0
]}},
“类型2”:{“$sum”:{“$cond”:[
{“$eq”:[“$\u id.type”,2]},
“$count”,
0
]}},
“类型3”:{“$sum”:{“$cond”:[
{“$eq”:[“$\u id.type”,3]},
“$count”,
0
]}}
}}
])
这正好给出:

{u id:“foobar”,“type1”:2,“type2”:1,“type3”:1}
实际上,我更喜欢有两个阶段的动态形式:

db.types.aggregate([
{“$组”:{
“_id”:{
“统计”:“$stat”,
“类型”:“$type”
},
“计数”:{“$sum”:1}
}},
{“$组”:{
“\u id”:“$\u id.stat”,
“类型”:{“$push”:{
“类型”:“$\u id.type”,
“计数”:“$count”
}}
}}
])
输出不相同,但功能强大,可灵活调整值:

{
“_id”:“foobar”,
“类型”:[
{
“类型”:3,
“计数”:1
},
{
“类型”:2,
“计数”:1
},
{
“类型”:1,
“计数”:2
}
]
}
否则,如果您需要相同的输出格式,但nee