然而,为了使用MongoDB的Spring Data / Java API,如何将以下mongo shell查询转换为Spring Data / Java查询,任何帮助都会很棒......
> db.collection.aggregate(
{$unwind : "$data.mi"},
{$sort : {"data.mi.failures" : -1}},
{$group : {_id : "$data.mi.name",
sum_total_of_failures : {$sum : "$data.mi.failures"},
uid : {$first : "$uid"}}}
)
答案 0 :(得分:2)
在春天,它看起来像这样
String collection="collection";
Aggregation agg = newAggregation(
unwind("data.mi")
,sort(DESC,"data.mi.failures")
,group("data.mi.name")
.sum("data.mi.failures").as("sum_total_of_failures")
.first("uid").as("uid")
);
AggregationResults<Type> aggregate = mongoOperations.aggregate(agg, collection, objectClass);
假设这些进口
import com.mongodb.BasicDBObject;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import static org.springframework.data.domain.Sort.Direction.*;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;