java mongo库根据特定字段去重


实体类 Entity,以字段code为例mogo库查询通用模板

Aggregation aggregation = setAggregation(Entity, 0);
List<Entity> resultList =
                mongoTemplate.aggregate(aggregation, "表名", Entity.class).getMappedResults();

  public Aggregation setAggregation(Entity entity,int type) {
        List<AggregationOperation> aggs = new ArrayList<>();


       分组并且筛选
 public Aggregation setAggregation(Entity entity,int type) {
  List<AggregationOperation> aggs = new ArrayList<>();

aggs.add(Aggregation.group("code").first("code").as("code"));
        

 aggs.add(Aggregation.sort(ASC, "code"));//排序
   

 aggs.add(Aggregation.project("code"));//要展示的字段

aggs.add(Aggregation.match(Criteria.where("code").ne(null)));//不等于null
        
        aggs.add(Aggregation.match(Criteria.where("code").ne("")));//不等于空字符串
模糊查询-相当于mysql  like
aggs.add(Aggregation.match(Criteria.where("name").regex("测试")));
      
       
        Integer pageSize = 20;
        Integer pageNumber = 1;
        if (type == 0) {
        这里处理分页
            if (pageNumber != null && pageSize != null) {
                aggs.add(Aggregation.skip((pageNumber - 1) * pageSize));
                aggs.add(Aggregation.limit(pageSize));
            }
        }
        if (type == 1) {
        计算数量
            aggs.add(Aggregation.count().as("totalCount"));
        }
        Aggregation aggregation = Aggregation.newAggregation(aggs)
                .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
        return aggregation;
    }

代码示例

Aggregation aggregation = setAggregation(entity, 0);
            List<Entity > resultList =
                mongoTemplate.aggregate(aggregation, "表名", Entity .class).getMappedResults();
           


 /**
     * @param Entity
     * @param type 0-分页查询、1-统计条数
     * @return
     */
    public Aggregation setAggregation(Entity entity,int type) {
        List<AggregationOperation> aggs = new ArrayList<>();

        aggs.add(Aggregation.group("code").first("code").as("code")
        );
        aggs.add(Aggregation.sort(ASC, "code"));
        aggs.add(Aggregation.project("code"));
        
        aggs.add(Aggregation.match(Criteria.where("code").ne(null)));
        aggs.add(Aggregation.match(Criteria.where("code").ne("")));

        if (entity.getCode() != null && !entity.getCode.equals("")) {
            aggs.add(Aggregation.match(Criteria.where("code").regex(entity.getCode)));
        }
        
        Integer pageSize = 20;
        Integer pageNumber = 1;
        if (type == 0) {
            if (pageNumber != null && pageSize != null) {
                aggs.add(Aggregation.skip((pageNumber - 1) * pageSize));
                aggs.add(Aggregation.limit(pageSize));
            }
        }
        if (type == 1) {
            aggs.add(Aggregation.count().as("totalCount"));
        }
        Aggregation aggregation = Aggregation.newAggregation(aggs)
                .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
        return aggregation;
    }

      
```

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值