使用dateToString 方法进行转换 并且通过format指定转换日期格式
Integer userId=aaa;
GroupOperation groupOperation = Aggregation.group("day").sum("money").as("todayIncome").count().as("todayPayCount");
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("userId").is(userId)),
project("userId","money").andExpression("{$dateToString: {date: { $add: {'$createTime', [0]} }, format: '%Y%m%d'}}", new Date(28800000)).as("day"),
groupOperation,
sort(Sort.Direction.ASC, "_id")
);
注意:
1.必须使用 $dateToString: {date: { $add: 通过求和进行date数据转换 如果去掉后面的会报解析错误
org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 16006 (Location16006): 'can't convert from BSON type lo