mongodb - Mongo aggregation of ISODate into 45 minute chunks -
mongodb - Mongo aggregation of ISODate into 45 minute chunks -
i have series of documents have isodate object. i'm trying grouping 45 min blocks. minutes 0-45 1 grouping , min 45-next min 30 another.
my first thought split time since these aren't unix timestamps i've been unable math isodate. sec thought $project out hour, minute, days, etc, realized challenging grouping when timeblock span 2 separate days.
# doesn't work since cant division. query = [ { "$match": { '_id.t': { '$gte': new date(2014, 1, 1, 1, 0, 0), '$lte': new date(2014, 6, 1, 1, 0, 0) } } }, { "$project": { "milliseconds": { '$millisecond': '$_id.t' } } }, { "$project": { "timeblock": { "$divide": [ '$milliseconds', 900000 ] } } }, { "$group": { "_id": { "timeblock": "$timeblock" } } } ]
i think next can solve question:
var basedate = new date(2014, 09, 17, 14, 25, 0); // value want var startdate = new date(2014, 09, 17, 14, 25, 0); var enddate = new date(2014, 09, 20, 14, 25, 0); var divisor = 45 * 60 * 1000; // milliseconds of 45 minutes db.cc.aggregate([ { $match : { date : { $gte : startdate, $lt : enddate } } }, { $group : { _id : { $subtract : [ "$date", { $mod : [ { $subtract : [ "$date", basedate ] }, divisor ] } ] }, dates : { $push : "$date" } } } ]).pretty();
explanation grouping:
_id : { $subtract : [ "$date", { /* start date of each block */ $mod : [ { /* remainder */ $subtract : [ "$date", basedate ] /* offset */ }, divisor ] } ] },
mongodb
Comments
Post a Comment