I'd personally perform a mapreduce on the collection :
map is a simple function emitting the "subida" field. The key should the same if you need a single sum; the result after reduce will yield the single object {<key>: <sum>}
, with <key>
being whatever value you supplied in the emit.
map = function() { emit(<key>, this.subida); }
reduce is also a simple function summing them :
red = function(k, v) {
var i, sum = 0;
for (i in v) {
sum += v[i];
}
return sum;
}
You can then call mapreduce on your collection <mycollection>
:
res = db.<mycollection>.mapReduce(map, red);
Which will create a temporary new collection you can manipulate like any other collection. The value returned by mapReduce holds several values regarding the mapReduce such as the time taken, status..., as well as the temp. collection name created in the "result" field.
To get the values you need, you have to query that collection :
db[res.result].find()
Which should give you the object {<key>: <sum>}
.
If you run MongoDB 1.7.4 or higher, you can save you a bit of hassle by asking MongoDB to return the result directly without creating a collection :
db.<mycollection>.mapReduce(map, red, {out : {inline: 1}});