obj63mc
10/20/2014 - 5:50 PM

Mongo DB Query to convert string to date and then aggregate by date.

Mongo DB Query to convert string to date and then aggregate by date.

Convert field from string to date -

db.post.find().forEach(function(p){p.postdate = new Date(p.postdate);db.post.save(p);});

Aggregate by that field -

db.post.aggregate([{$project:{day:{$dayOfMonth:'$postdate'},month:{$month:'$postdate'},year:{$year:'$postdate'}}}, {$group:{_id:{day:'$day',month:'$month',year:'$year'}, count: {$sum:1}}}])