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.