julianusti
12/10/2013 - 8:56 AM

gistfile1.js

function getStatusForThePastHour(id, callback) {
	
	var to = moment().format();
	var from = moment(to).subtract('hours', 1).format();

	db.venues.aggregate([
		{ $match: { "_id": id } },
		{ $unwind: '$loads' },
		{ $match: { "loads.timeStamp": { $gte: from, $lte: to} }
		},
		{ $group: {
			_id: "$loads.status",
			count: {$sum: 1}
		}},
		{ $project:
			{
				_id: 0,
				status: "$_id",
				count: 1
			}
		}
	], function(err, status) {
		if(err) {
			return callback(err);
		}

		return callback(null, status);
	});
}