After trying this solution and getting one step further i have another question regarding mongodb.
My question is:
How can i sort the output of:
doc = {_id : 16, days : { 1 : 123, 2 : 129, 3 : 140, 4 : 56, 5 : 57, 6 : 69, 7 : 80 }};
db.so.insert(doc);
map = function() {
emit(this._id, this.days["1"]);
emit(this._id, this.days["3"]);
emit(this._id, this.days["7"]);
}
reduce = function (k, vals) {
var sum = 0;
vals.forEach(function (v) {sum += v;});
return sum;
}
res = db.so.mapReduce(map, reduce, {out : {inline : 1}});
res.find();
The Output is like this:
"results" : [
{
"_id" : 16,
"value" : 225
},
{
"_id" : 33,
"value" : 230
},
{
"_id" : 302,
"value" : 274
}
]
Now i want to sort the result with:
res.find().sort({ "results.value":-1 });
which results in this error:
Sat Mar 31 01:15:45 TypeError: res.find().sort({'results.value':-1}) is not a function (shell):1
Does anybody can help me ?
Here’s an example of the same query using the MongoDB 2.2 Aggregation Framework:
.. and the output: