Skip to content

Instantly share code, notes, and snippets.

@ffaerber
Created March 12, 2014 10:58
Show Gist options
  • Save ffaerber/9504721 to your computer and use it in GitHub Desktop.
Save ffaerber/9504721 to your computer and use it in GitHub Desktop.
db.measurements.aggregate([
{ "$match" : { "timestamp": { "$gte": ISODate('2014-03-12 00:00:00'), "$lt": ISODate('2014-03-12 23:59:59') } } },
{ "$project": {
"wh": 1,
"hourly": { "$hour": "$timestamp" }
}},
{ "$sort": { "hourly": 1 } },
{ "$group": {
"_id": "$hourly",
"firstReading": { "$first": "$wh" },
"lastReading": { "$last": "$wh" }
}},
{ "$project": {
"hourReading": { "$subtract": [ "$lastReading" - "$firstReading" ] }
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment