請查看下面蒙戈外殼查詢,因爲它採用的聚合框架的功率,每小時簡單analitics:
這將產生以下的輸出:
{
"_id" : {
"year" : 2016,
"month" : 5,
"day" : 1,
"hour" : 20
},
"avgPrice" : 10.5,
"min" : 10.0,
"max" : 11.0,
"firstEntryValue" : 10.0,
"lastEntrValue" : 11.0,
"allEntries" : [
{
"timestamp" : ISODate("2016-05-01T20:00:00.000Z"),
"value" : 10.0,
"_id" : ObjectId("57610dce41bcea24b9792443")
},
{
"timestamp" : ISODate("2016-05-01T20:01:00.000Z"),
"value" : 11.0,
"_id" : ObjectId("57610dd641bcea24b9792444")
},
{
"timestamp" : ISODate("2016-05-01T20:01:03.000Z"),
"value" : 10.0,
"_id" : ObjectId("57610dde41bcea24b9792445")
},
{
"timestamp" : ISODate("2016-05-01T20:01:05.000Z"),
"value" : 11.0,
"_id" : ObjectId("57610de341bcea24b9792446")
}
]
}
查詢示例
var projectTime = {
$project : {
_id : 1,
symbol : 1,
price : 1,
timestamp : 1,
// now we need to add year/month/day/hour
// having this field as separate ones will help us to have diffrent groupping options
year : {
$year : "$timestamp"
},
month : {
$month : "$timestamp"
},
day : {
$dayOfMonth : "$timestamp"
},
hour : {
$hour : "$timestamp"
},
}
}
var group = {
$group : {
_id : {
year : "$year",
month : "$month",
day : "$day",
hour : "$hour"
},
avgPrice : {
$avg : "$price"
},
min : {
$min : "$price"
},
max : {
$max : "$price"
},
firstEntryValue : {
$first : "$price"
},
lastEntrValue : {
$last : "$price"
},
allEntries : {
$push : {
timestamp : "$timestamp",
value : "$price",
_id : "$_id"
}
},
}
}
db.casy.aggregate([projectTime, group])
這正是即時尋找,謝謝!你真是太棒了,謝謝你的時間,真的。 – Casy
這是我的榮幸〜 – Leo