Question

Hello I am new to mongodb and trying to convert objects with different types (int) into key value pairs.

I have collection like this:

{
    "_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"
}

I want to get result like this:

{
    "type1" : 2, "type2" : 1, "type3" : 1,
    "stat" : "foobar"
}

Currently trying aggregation group and then push type values to array

db.types.aggregate(
    {$group : {
        _id : "$stat",
        types : {$push : "$type"}
    }}
)

But don't know how to sum different types and to convert it into key values

/* 0 */
{
    "result" : [ 
        {
            "_id" : "foobar",
            "types" : [ 
                1, 
                2, 
                2, 
                3
            ]
        }
    ],
    "ok" : 1
}
Was it helpful?

Solution

For your actual form, and therefore presuming that you actually know the possible values for "type" then you can do this with two $group stages and some use of the $cond operator:

db.types.aggregate([
    { "$group": {
         "_id": {
             "stat": "$stat",
             "type": "$type"
         },
         "count": { "$sum": 1 }
    }},
    { "$group": {
        "_id": "$_id.stat",
        "type1": { "$sum": { "$cond": [
            { "$eq": [ "$_id.type", 1 ] },
            "$count",
            0
        ]}},
        "type2": { "$sum": { "$cond": [
            { "$eq": [ "$_id.type", 2 ] },
            "$count",
            0
        ]}},
        "type3": { "$sum": { "$cond": [
            { "$eq": [ "$_id.type", 3 ] },
            "$count",
            0
        ]}}
    }}
])

Which gives exactly:

{ "_id" : "foobar", "type1" : 2, "type2" : 1, "type3" : 1 }

I actually prefer the more dynamic form with two $group stages though:

db.types.aggregate([
    { "$group": {
         "_id": {
             "stat": "$stat",
             "type": "$type"
         },
         "count": { "$sum": 1 }
    }},
    { "$group": {
        "_id": "$_id.stat",
        "types": { "$push": {
            "type": "$_id.type",
            "count": "$count"
        }}
    }}
])

Not the same output but functional and flexible to the values:

{
    "_id" : "foobar",
    "types" : [
            {
                    "type" : 3,
                    "count" : 1
            },
            {
                    "type" : 2,
                    "count" : 1
            },
            {
                    "type" : 1,
                    "count" : 2
            }
    ]
}

Otherwise if you need the same output format but need the flexible fields then you can always use mapReduce, but it's not exactly the same output.

db.types.mapReduce(
    function () {

        var obj = { };

        var key = "type" + this.type;
        obj[key] = 1;

        emit( this.stat, obj );

    },
    function (key,values) {

        var obj = {};

        values.forEach(function(value) {
            for ( var k in value ) {
                if ( !obj.hasOwnProperty(k) )
                    obj[k] = 0;
                obj[k]++;
            }
        });

        return obj;

    },
    { "out": { "inline": 1 } }
)

And in typical mapReduce style:

   "results" : [
            {
                    "_id" : "foobar",
                    "value" : {
                            "type1" : 2,
                            "type2" : 1,
                            "type3" : 1
                    }
            }
    ],

But those are your options

OTHER TIPS

Is this close enough for you?

{ "_id" : "foobar", "types" : [ { "type" : "type3", "total" : 1 }, { "type" : "type2", "total" : 1 }, { "type" : "type1", "total" : 2 } ] }

The types are in an array, but it seems to get you the data you are looking for. Code is:

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" } } }}
   ]
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top