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

如何使用spring mongodb聚合函数表示ohlc mongo查询,mongodb,spring-data-mongodb,mongodb-java,Mongodb,Spring Data Mongodb,Mongodb Java,我正在为股票编写ohlc查询,数据如下: { "_id" : "52f97cba03646d746011199a" , "tradedAmount" : { "amount" : 1050000000 , "currency" : "USD"} , "tradedPrice" : { "amount" : 10500 , "currency" : "CNY"} , "orderBookIdentifier" : "1024d9ab-3c2a-4c06-a127-9ab717aa2474" , "

我正在为股票编写ohlc查询,数据如下:

{ "_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聚合=newAggregation(TradeExecutedEntry.class,
匹配(其中(“orderBookIdentifier”)。是(orderBookIdentifier)
和(“交易时间”).gte(开始)
。及(“交易时间”)。lt(完),
项目(“交易时间”、“交易价格”、“交易金额”)
以及(“交易时间”)项目(“年”)作为(“年”)
和(“交易时间”)项目(“月”)作为(“月”)
和(“交易时间”)项目(“月日”)作为(“日”)
以及(“交易时间”)项目(“小时”)作为(“小时”),
排序(描述,“交易时间”、“交易价格金额”),
组(Fields.from(Fields.field(“priceCurrency”、“tradedPrice.currency”))
和(Fields.field(“amountCurrency”、“tradedAmount.currency”))
和(字段字段(“年”))
和(字段字段(“月”、“月”))
和(字段字段(“日”、“日”))
和(字段字段(“小时”、“小时”))
)
.第一(“贸易价格”)作为(“公开”)
.max(“贸易价格”)。作为(“高”)
.min(“贸易价格”).as(“低”)
.最后(“交易价格”)。作为(“结束”)
.总额(“交易量”)。作为(“交易量”),
跳过(pageable.getOffset()),
限制(pageable.getPageSize())
);
AggregationResults=mongoTemplate.aggregate(聚合,OpenHighLowCloseVolume.class);

有人能帮我一下吗?

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

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"));
祝你好运

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"));