我一直在研究一个我正在开发的项目的类似问题,很高兴看到我得出了类似的结论。
我的传入数据来自Robinhood的API;它看起来与您的类似,但给了我字符串中的日期戳,所以我添加了$to date和$to string来将字符串转换为日期对象。我可以选择在ruby中将传入的日期戳转换为一个时间对象,然后删除这里的额外工作。
我仍然在完善我的分组,因为我不确定我是否真的需要两个。我的传入流有时会复制数据(每秒,由于字符串日期戳的情况),因此第一组要清除该数据,以避免在第二组中虚报{“$sum”:“$volume”}。
我的$project在被请求的前一分钟的59秒内返回我的100万烛台数据密钥与Robinhood的API历史匹配,因此我可以在代码中将它们视为相同的对象。
var date_now = new Date(new ISODate("2019-10-21T20:00:00.000Z"))
var date_end = new Date(date_now.getTime() - 1000 * 1)
var date_begin = new Date(date_now.getTime() - 1000 * 60 * 10)
db.quotes.aggregate([
{ "$addFields": { "date": { "$toDate": "$time_pulled" } } },
{ $match: { symbol: "NUGT",
date: { "$gte": date_begin,
"$lt": date_end } } },
{ $group: { _id: "$date", // group by date to eliminate dups
"symbol": { "$first": "$symbol" },
"price": { "$max": "$last_trade_price" },
"volume": { "$max": "$volume" } } },
{ $sort: { _id: 1 } }, // sort by date to get correct first/last prices
{ $group: { _id: "$symbol",
//"date_begin": { "$first": { "$toString": "$_id" } },
//"date_end": { "$last": { "$toString": "$_id" } },
"high_price": { "$max": "$price" },
"low_price": { "$min": "$price" },
"open_price": { "$first": "$price" },
"close_price": { "$last": "$price" },
"volume": { "$sum": "$volume" } } },
{ $project: { _id: 1,
begins_at: { "$toString": date_begin },
volume: 1,
high_price: 1,
low_price: 1,
open_price: 1,
close_price: 1 } }
])
结果:
/* 1 */
{
"_id" : "NUGT",
"high_price" : "26.860000",
"low_price" : "26.740000",
"open_price" : "26.834200",
"close_price" : "26.820000",
"volume" : 392086.0,
"begins_at" : "2019-10-21T19:50:00.000Z"
}