Spring Data 1.3.0.RC1可用,它支持聚合框架。
例如: 外殼聚集COMAND:
db.eft_transactions.aggregate(
{$match:
{
service:"EFT",
source:"MARKUP",
}
},
{$group:
{
_id:"$card_acceptor_id",
tran_count:{$sum:1},
amount_sum:{$sum:"$amount"}
}
}
)
從Java運行是這樣的:
AggregationOperation match = Aggregation.match(Criteria.where("service").is("EFT").and("source").is("MARKUP"));
AggregationOperation group = Aggregation.group("card_acceptor").and("amount_sum").sum("amount").and("tran_count").count();
Aggregation aggregation = newAggregation(match, group);
AggregationResults<StoreSummary> result = this.mongoTemplate.aggregate(aggregation, "eft_transactions", StoreSummary.class);
的文檔here
注意:我們最近不得不改用使用版本1.3.0的BUILD-SNAPSHOT構建。此更改需要更改以上兩行中的更改爲:
AggregationOperation group = Aggregation.group("card_acceptor").sum("amount").as("amount_sum").count().as("tran_count");
Aggregation aggregation = Aggregation.newAggregation(match, group);
感謝澄清這對我來說。 – Andna 2013-03-26 07:04:37
但是,只要您需要聚合框架的強大功能,就可以向mongodb Java驅動程序邁出一步。 'mongoOps.getCollection(「yourCollection」)。aggregate(...)' – 2013-03-28 14:54:50
感謝這些信息,它可能派上用場。 – Andna 2013-03-28 19:00:00