一、投影表字段
投影只包含a_field和b_field字段
Aggregation aggregation= Aggregation.newAggregation(Aggregation.project("a_field", "b_field"));
List<Map> list = mongoOperations.aggregate(aggregation, "table", Map.class).getMappedResults();
二、投影排除字段
排除a_field和b_field字段,投影其他字段
Aggregation aggregation= Aggregation.newAggregation(Aggregation.project().andExclude("a_field", "b_field"));
List<Map> list = mongoOperations.aggregate(aggregation, "table", Map.class).getMappedResults();
三、投影表达式
时间毫秒time字段投影生成总秒数seconds和小时余数hourMod字段
Aggregation aggregation= Aggregation.newAggregation(Aggregation.project().and(ArithmeticOperators.Floor.floorValueOf(ArithmeticOperators.Divide.valueOf("time").divideBy(1000))).as("seconds").and(ArithmeticOperators.Mod.valueOf(ArithmeticOperators.Floor.floorValueOf(ArithmeticOperators.Divide.valueOf("time").divideBy(1000))).mod(24 * 60 * 60)).as("hourMod"));List<Map> list = mongoOperations.aggregate(aggregation, "table", Map.class).getMappedResults();