obj63mc
11/15/2017 - 4:36 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({postdate:{$type:2}}).sort({postdate:1}).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}}}])