How can I translate the following Sql
query for Mongo
?:
select a,b,sum(c) csum from coll wh
I built up a histogram and what I did with version 2.2.2 was:
answer = db.coll.group(...)
db.histo.insert(answer)
db.histo.find().sort({ field: 1 })
At this point, if you don't need it, just db.histo.drop()
.
You can also avoid the variable and do:
db.histo.insert(db.coll.group(...))
db.histo.ensureIndex({ field: 1 })
db.coll.group(
{key: { a:true, b:true },
cond: { active:1 },
reduce: function(obj,prev) { prev.csum += obj.c; },
initial: { csum: 0 }
});
You can execute it in MongoDB