I have this MongoDB group-by query which works great from the Mongo console:
db.accounts.group(
{ cond: { "created_at" : { $gte: ISODate("2012-08-12T00:00:00Z") }}
, key: {member_id: true}
, initial: {count: 0, failed: 0, succeeded: 0}
, reduce: function(doc, out) {
out.count++;
if (!doc.success)
out.failed++
else
out.succeeded++
}
});
My three questions:
- Is there an easier way of generating this timestamp: ISODate("2012-08-12T00:00:00Z")? Can I use a date function to generate this timestamp? If so, how?
- How can I sort (asc/desc) the returned results by the grouped fields: "failed" or "succeeded"?
- Finally, how would I export the output to a flat file?
Update
For #3, I was basically looking for this:
mongo my_db --eval 'some_var=X;' --quiet my_script.js >> output.txt
Where my_db is the database I'm using, some_var is a variable I'm passing to my_script.js which contains my group-by query. output.txt is obviously where the output is written.