This is a question about the best way to add up a series of data in an array where I have to match another element. I'm trying to use the 2.2 Aggregation framework and it's possible I can do this with a simple group.

So for a given set of documents I'm trying to get an output like this;

{
    "result" : [
            {
                "_id" : null,
                "numberOf": 2,
                "Sales" : 468000,
                "profit" : 246246,
            }
    ],
    "ok" : 1
}

Now, I originally had a list of documents, containing values assigned to named properties, eg;

[
{
    _id : 1,
    finance: {
        sales: 234000,
        profit: 123123,
    }
}
,
{
    _id : 2,
    finance: {
        sales: 234000,
        profit: 123123,
    }
}
]

This was easy enough to add up, but the structure didn't work for other reasons. For instance, there are may other columns like "finance" and I want to be able to index them without creating thousands of indexes, so I need to convert to a structure like this;

[
{
    _id : 1,
    finance: [
        {
            "k": "sales",
            "v": {
                "description":"sales over the year",
                v: 234000,
            }
        },
        {
            "k": "profit",
            "v": {
                "description":"money made from sales",
                v: 123123,
            }
        }
    ]
}
,
{
    _id : 2,
    finance: [
        {
            "k": "sales",
            "v": {
                "description":"sales over the year",
                v: 234000,
            }
        },
        {
            "k": "profit",
            "v": {
                "description": "money made from sales",
                v: 123123,
            }
        }
    ]
}
]

I can index finance.k if I want, but then I'm struggling to build an aggregate query to add up all the numbers matching a particular key. This was the reason I originally went for named properties, but this really needs to work in a situation whereby there are thousands of "k" labels.

Does anyone know how to build an aggregate query for this using the new framework? I've tried this;

db.projects.aggregate([
    {
        $match: {
            // QUERY
            $and: [
                // main query
                {},
            ]
        }
    },
    {
        $group: {
            _id: null,
            "numberOf": { $sum: 1 },
            "sales":    { $sum: "$finance.v.v" },
            "profit":   { $sum: "$finance.v.v" },
        }
    },
])

but I get;

{
    "errmsg" : "exception: can't convert from BSON type Array to double",
    "code" : 16005,
    "ok" : 0
}

** For extra kudos, I'll need to be able to do this in a MapReduce query as well.

Solution 1

You can use the aggregation framework to get sales and profit and any other value you may be storing in your key/value pair representation.

For your example data:

var pipeline = [
    {
        "$unwind" : "$finance"
    },
    {
        "$group" : {
            "_id" : "$finance.k",
            "numberOf" : {
                "$sum" : 1
            },
            "total" : {
                "$sum" : "$finance.v.v"
            }
        }
    }
]

R = db.tb.aggregate( pipeline );
printjson(R);
{
        "result" : [
            {
                "_id" : "profit",
                "numberOf" : 2,
                "total" : 246246
            },
            {
                "_id" : "sales",
                "numberOf" : 2,
                "total" : 468000
            }
        ],
        "ok" : 1
}

If you have additional k/v pairs then you can add a match which only passes through k values in ["sales","profit"].

Solution 2

You will have to use '$unwind" to break out the values in the array, which will mean that you can't get the sum of the sales and the profit in a single aggregation command. Given that, the query itself is easy:

var pipeline = [ 
        {"$unwind": "$finance" } ,
        {"$match": {"finance.k": "sales"} },
        { $group: 
            { _id: null,
                numberOf: { "$sum": 1 },
                sales: {"$sum": "$finance.v.v" }
            }
        }
    ];

R = db.tb.aggregate( pipeline );
printjson(R);

{
        "result" : [
                {
                        "_id" : null,
                        "numberOf" : 2,
                        "sales" : 236340
                }
        ],
        "ok" : 1
}

You can run a similar query for profit, just substitute "profit" for "sales" in the "$match" operator.

Oh, and here's the map/reduce example:

map = function() {
    var ret = { sales: 0.0 , profit: 0.0, count: 1 };

    // iterate over 'finance[]' array
    this.finance.forEach( function (i) { 
        if ( i.k == "sales" ) ret.sales =  i.v.v ;
        if ( i.k == "profit" ) ret.profit =  i.v.v ;
    } );

    emit( 1, ret );
}

reduce = function(key, values) {
    var ret = { sales: 0.0 , profit: 0.0, count: 0 };

    values.forEach(function(v) {
        ret.sales += v.sales;
        ret.profit += v.profit;
        ret.count += v.count;
    });

    return ret;
    };
//
// run map/reduce
//
res = SOURCE.mapReduce( map, reduce );