spring mongodb mongoTemplate管道查询,时间排序出错

文章讨论了在MongoDB中进行时间排序时遇到的问题,强调了时间字段的格式一致性对正确排序的重要性。当时间格式不统一,如2023/04/1110:04和2023/4/1110:4共存时,可能导致排序错误。解决方案是确保所有时间戳的格式严格相同。
摘要由CSDN通过智能技术生成

代码: 

    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,但还没有那么智能到能检查出来,当这两个时间都存在一个表里面的时候,整个表的时间排序就会出错,无法正确排序。

1. 使用mongoTemplate进行联表查询 MongoDB中使用$lookup运算符实现联表查询。在Spring Data MongoDB中,可以通过Aggregation类来构建复杂的聚合查询。 例如,假设有两个集合:orders和products,它们的文档结构分别如下: orders { "orderId": 1, "productId": 1, "quantity": 5 }, { "orderId": 2, "productId": 2, "quantity": 3 } products { "productId": 1, "name": "iPhone", "price": 999 }, { "productId": 2, "name": "iPad", "price": 799 } 现在需要通过orderId查询订单详情,并将订单详情和商品信息联合在一起。可以使用以下代码实现: // 构建Aggregation查询 Aggregation agg = newAggregation( match(Criteria.where("orderId").is(orderId)), // 匹配订单ID lookup("products", "productId", "productId", "product"), // 联表查询 unwind("product"), // 展开查询结果 project("orderId", "quantity", "product.name", "product.price") // 投影查询结果 ); // 执行查询 AggregationResults<OrderDetail> results = mongoTemplate.aggregate(agg, "orders", OrderDetail.class); 其中OrderDetail是一个包含订单详情和商品信息的DTO类: public class OrderDetail { private Integer orderId; private Integer quantity; private String productName; private Double productPrice; // getter和setter方法 } 2. 使用mongoTemplate查询结果排序 在上述联表查询的基础上,可以通过以下代码对查询结果按照商品价格进行升序排序: Aggregation agg = newAggregation( match(Criteria.where("orderId").is(orderId)), lookup("products", "productId", "productId", "product"), unwind("product"), project("orderId", "quantity", "product.name", "product.price"), sort(Sort.Direction.ASC, "product.price") // 排序 ); AggregationResults<OrderDetail> results = mongoTemplate.aggregate(agg, "orders", OrderDetail.class); 其中sort方法用来指定排序方式,第一个参数为排序方向,第二个参数为排序字段。在这里,我们按照product.price字段进行升序排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值