vkl*_*ein 5 spring aggregation mongodb
我想在 Spring Boot 应用程序中获取此 MongoDB 查询的结果。
db.getCollection('contentSource').aggregate( [ { $sort: { "modified": -1 } },
{ $group: { _id: "$sourceId", cs: { $push: "$$ROOT" } }},
{ $replaceRoot: { newRoot: { $arrayElemAt: ['$cs', 0] } }} ] )
Run Code Online (Sandbox Code Playgroud)
有谁知道如何将 replaceRoot 添加到我的聚合中?
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();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1570 次 |
最近记录: |