假设我的收集定义如下:
@Document(collection = "Item")
public class Item {
@Id
private String id;
private String title;
private String mrp;
private String discount;
//getters and setters goes here
}
我需要找到所有项,这些项目按[mrp - discount]值排序。
如何使用MongoOperations
表达这一点答案 0 :(得分:1)
这就是Aggregation Framework的用途。 Spring Data MongoDB通过aggregate
提供MongoOperations
。
TypedAggregation<Item> agg = newAggregation(Item.class,
project()
.andExpression("mrp - discount").as("total")
.andInclude("mrp", "discount", "id"),
sort(new Sort(ASC, "total")));