mongodb烛台查询
我需要从mongo查询财务数据并生成每日蜡烛棒图 集合架构类似于:mongodb烛台查询,mongodb,candlestick-chart,Mongodb,Candlestick Chart,我需要从mongo查询财务数据并生成每日蜡烛棒图 集合架构类似于: { symbol: 'GOOG', amount: 1000, rate: 123, created_at: ISODate('some point in time') } 图表中的每个条目(即每给定一天)应由4个值组成: 高(最高(比率)) 低(最低(速率)) 开放式(给定日期内的第一流) 关闭(给定日期中的最后一个费率) 知道如何构建查询吗?好的,在发送给我后,我了解了: 我一直在对我正在开发的一个项目进行
{
symbol: 'GOOG',
amount: 1000,
rate: 123,
created_at: ISODate('some point in time')
}
图表中的每个条目(即每给定一天)应由4个值组成:
知道如何构建查询吗?好的,在发送给我后,我了解了:
我一直在对我正在开发的一个项目进行类似的查询,很高兴看到我得出了类似的结论 我输入的数据来自Robinhood的API;它看起来与您的类似,但提供了字符串形式的日期戳,因此我添加了$toDate和$toString以将字符串翻转到日期对象。我可以选择将传入的日期戳转换为Ruby中的时间对象,然后在这里删除额外的工作 我仍然在改进我的分组,因为我不确定我是否真的需要两个。我的传入流有时会复制数据(由于字符串日期戳的情况,每秒重复一次),因此第一组将清理数据,以避免错误地膨胀第二组中的{“$sum”:“$volume”} 我的$project在请求的前一分钟59秒内返回我的1m烛台数据。这些键匹配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"
}
/* 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"
}