如何使用spring mongodb聚合函数表示这个ohlc mongo查询

时间:2014-02-11 04:38:54

标签: mongodb spring-data-mongodb mongodb-java

我正在对股票进行查询,数据如下:

{ "_id" : "52f97cba03646d746011199a" , "tradedAmount" : { "amount" : 1050000000 , "currency" : "USD"} , "tradedPrice" : { "amount" : 10500 , "currency" : "CNY"} , "orderBookIdentifier" : "1024d9ab-3c2a-4c06-a127-9ab717aa2474" , "tradeTime" : ISODate("2011-11-30T04:12:12.120Z") , "tradeType" : "BUY" , "audit" : { "created_by" : null , "created_on" : ISODate("2014-02-11T01:28:26.720") , "last_modified_by" : null } , "version" : 0})

聚合js脚本是:

db.tradeExecutedEntry.aggregate(
[
    { "$match" : { "orderBookIdentifier" : "1024d9ab-3c2a-4c06-a127-9ab717aa2474" ,
        "tradeTime" : { "$gte" : ISODate("2006-12-12T04:12:12.120Z") , "$lt" : ISODate("2015-12-12T04:12:04.120Z")}}} ,
    { "$project" :
        {"tradeTime" : 1 , "tradedPrice" : "$tradedPrice.amount" , "priceCurrency" : "$tradedPrice.currency" ,
        "tradedAmount" : "$tradedAmount.amount" , "tradeCurrency" : "$tradedAmount.currency" ,
        "year" : { "$year" : [ "$tradeTime"]} ,
        "month" : { "$month" : [ "$tradeTime"]} ,
        "day" : { "$dayOfMonth" : [ "$tradeTime"]} ,
        "hour" : { "$hour" : [ "$tradeTime"]}}},
    { $sort : { tradeTime : 1, "tradedPrice.amount":-1} },
    {"$group":
        {"_id" : {"year": "$year", "month": "$month", "day": "$day", "hour": "$hour", "minute": "$minute" },
        "open": {"$first": "$tradedPrice"},
        "high": {"$max": "$tradedPrice"},
        "low": {"$min": "$tradedPrice"},
        "close": {"$last": "$tradedPrice"},
        "volume" : { "$sum" : "$tradedAmount"}}}

]
);

如何使用聚合框架来表示它?以下不能正常工作(它返回一个空条目):

TypedAggregation<TradeExecutedEntry> aggregation = newAggregation(TradeExecutedEntry.class,
              match(where("orderBookIdentifier").is(orderBookIdentifier)
                      .and("tradeTime").gte(start)
                      .and("tradeTime").lt(end)),
              project("tradeTime", "tradedPrice", "tradedAmount")
                      .and("tradeTime").project("year").as("year")
                      .and("tradeTime").project("month").as("month")
                      .and("tradeTime").project("dayOfMonth").as("day")
                      .and("tradeTime").project("hour").as("hour")                ,
              sort(DESC, "tradeTime", "tradedPrice.amount"),
              group(Fields.from(Fields.field("priceCurrency", "tradedPrice.currency"))
                      .and(Fields.field("amountCurrency", "tradedAmount.currency"))
                      .and(Fields.field("year", "year"))
                      .and(Fields.field("month", "month"))
                      .and(Fields.field("day", "day"))
                      .and(Fields.field("hour", "hour"))
              )
                      .first("tradedPrice").as("open")
                      .max("tradedPrice").as("high")
                      .min("tradedPrice").as("low")
                      .last("tradedPrice").as("close")
                      .sum("tradedAmount").as("volume"),
              skip(pageable.getOffset()),
              limit(pageable.getPageSize())
      );
AggregationResults<OpenHighLowCloseVolume> result = mongoTemplate.aggregate(aggregation, OpenHighLowCloseVolume.class);

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

我尝试了很多次,这对我有用:

Aggregation chartAgg = Aggregation.newAggregation(
    Aggregation.match(Criteria.where("investorId").is(user.getId()).and("status").is(Constant.Terms.Status.RETURNING )),
    Aggregation.project("profit", "status").and("date").project("month").as("key"),
    Aggregation.group("key", "status").sum("profit").as("profit"),
    Aggregation.project("key", "profit"));
祝你好运!