I want to get the result of this MongoDB query in a Spring Boot Application.
db.getCollection('contentSource').aggregate( [ { $sort: { "modified": -1 } },
{ $group: { _id: "$sourceId", cs: { $push: "$$ROOT" } }},
{ $replaceRoot: { newRoot: { $arrayElemAt: ['$cs', 0] } }} ] )
Does anyone know how to add the replaceRoot to my Aggregation?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With