Spring Boot MongoDB聚合与ReplaceRoot/如何获取组中的最新项
我想在Spring引导应用程序中获得这个MongoDB查询的结果Spring Boot MongoDB聚合与ReplaceRoot/如何获取组中的最新项,spring,mongodb,aggregation,Spring,Mongodb,Aggregation,我想在Spring引导应用程序中获得这个MongoDB查询的结果 db.getCollection('contentSource').aggregate( [ { $sort: { "modified": -1 } }, { $group: { _id: "$sourceId", cs: { $push: "$$ROOT" } }}, { $replaceRoot: { newRoot: { $arrayElemAt: ['$cs', 0] } }} ] ) 有人知道如何将replaceR
db.getCollection('contentSource').aggregate( [ { $sort: { "modified": -1 } },
{ $group: { _id: "$sourceId", cs: { $push: "$$ROOT" } }},
{ $replaceRoot: { newRoot: { $arrayElemAt: ['$cs', 0] } }} ] )
有人知道如何将replaceRoot添加到我的聚合中吗?sortoOperation sortoOperation=new sortoOperation(new Sort(Sort.Direction.ASC,“modified”);
SortOperation sortOperation = new SortOperation(new Sort(Sort.Direction.ASC,"modified"));
GroupOperation groupOperation = group("sourceId").push("$$ROOT").as("cs");
Aggregation aggregation = newAggregation( sortOperation , groupOperation);
AggregationOperation replaceRoot = Aggregation.replaceRoot().withValueOf(ArrayOperators.ArrayElemAt.arrayOf("cs").elementAt(0));
AggregationResults<Document> result = mongoTemplate.aggregate(aggregation,"contentSource", replaceRoot, Document.class);
return result.getMappedResults();
GroupOperation GroupOperation=group(“sourceId”).push($$ROOT”).as(“cs”);
聚合聚合=新聚合(排序操作、组操作);
AggregationOperation replaceRoot=Aggregation.replaceRoot(),其值为(ArrayOperators.ArrayElemAt.arrayOf(“cs”).elementAt(0));
AggregationResults=mongoTemplate.aggregate(聚合,“contentSource”、replaceRoot、Document.class);
返回result.getMappedResults();