代码:
public Map<String, Object> getMonList(String id, int pageIndex, int pageSize) {
int start=0;
if(pageIndex!=0){
start=pageIndex*pageSize+1;
}
Sort sort = Sort.by(Sort.Direction.DESC,"time");
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.lookup("user", "otherId", "id", "user"),
Aggregation.match(Criteria.where("myId").is(id)),
Aggregation.sort(sort),
// Aggregation.group("sendId", "user.head", "user.name"),
Aggregation.project("myId", "otherId","user.head", "user.name","message","time","redCount"),
// Aggregation.unwind("user.head", "user.name"),
Aggregation.skip(Long.valueOf(start)),
Aggregation.limit((long) pageSize)
);
List<MessageMonList> returns = mongoTemplate.aggregate(aggregation, "messageMonList", MessageMonList.class).getMappedResults();
return jsonUtil.successReturns(returns);
}
数据表结构:
自定义时间排序时,mongodb是可以正确排序的,但必须要保证要排序的时间长度、格式是一致的,如:2023/04/11 10:04 和 2023/4/11 10:4 ,虽然少了0,但还没有那么智能到能检查出来,当这两个时间都存在一个表里面的时候,整个表的时间排序就会出错,无法正确排序。