代码之家  ›  专栏  ›  技术社区  ›  shaharsol

MongoDB烛台查询

  •  3
  • shaharsol  · 技术社区  · 7 年前

    我需要从mongo查询财务数据并生成每日蜡烛棒图。

    集合架构类似于:

    {
      symbol: 'GOOG',
      amount: 1000,
      rate: 123,
      created_at: ISODate('some point in time')
    }
    

    图表中的每个条目(即每天)应包含4个值:

    1. 高(最大速率)
    2. 低(最小速率)
    3. 开放(在给定的一天内是第一流的)
    4. 收盘价(某一天的最后价格)

    知道如何构建查询吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   shaharsol    7 年前

    好吧,我想是在 @David Peleg 派我来的 this repo :

    db.collection.aggregate([
          {$match:{
            symbol:'GOOG'
          }},
          {$project:{
            "day":{
              "y": {"$year":"$created_at"},
              "m": {"$month":"$created_at"},
              "d": {"$dayOfMonth": "$created_at"}
            },
            created_at: 1,
            rate: 1
          }},
          {"$sort":{"created_at":1}},
          {"$group":{
            "_id": "$day",
            "created_at": {"$first":"$created_at"},
            "open": {"$first":"$rate"},
            "close": {"$last":"$rate"},
            "high": {"$max":"$rate"},
            "low": {"$min":"$rate"},
          }},
          {$project:{
            "_id": "$_id",
            "rates":{
              "open": "$open",
              "close": "$close",
              "high": "$high",
              "low": "$low"
            }
          }},
          {"$sort":{"_id":1}}
        ])
    
        2
  •  1
  •   danarchy85    5 年前

    我一直在研究一个我正在开发的项目的类似问题,很高兴看到我得出了类似的结论。

    我的传入数据来自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"
    }
    
    推荐文章