mongo java查询前十条_java中mongo的条件查询

@Override

public Page findAll(Pageable pageable, ProductInfo productInfo) {

//创建一个操作聚合operations

List operations = new ArrayList<>();

//创建一个条件类criteria

Criteria criteria = new Criteria();

//商品状态不为空

if (productInfo.getProductStatus()!=null){

//productStatus等于查询的商品状态这个条件添加进操作聚合operations

operations.add(Aggregation.match(criteria.and("productStatus").is(productInfo.getProductStatus())));

}

long totalCount = 0;

//总页数

if (operations!= null && operations.size() > 0){

//操作聚合

Aggregation aggregationCount = newAggregation(operations);

//查询总的数据条数,返回一个集合

AggregationResults resultsCount = mongoTemplate.aggregate(aggregationCount, "productInfo", ProductInfo.class);

totalCount = resultsCount.getMappedResults().size();

}else{

//操作聚合为空,查询总的数据条数

totalCount = mongoTemplate.findAll(ProductInfo.class).size();

}

//操作聚合添加页数乘以每页大小等于总页数

operations.add(Aggregation.skip((long) (pageable.getPageNumber()) * pageable.getPageSize()));

//操作聚合添加返回一页的数据

operations.add(Aggregation.limit(pageable.getPageSize()));

//操作聚合添加根据solveCount降序排序

// operations.add(Aggregation.sort(Sort.Direction.DESC, "solveCount"));

Aggregation aggregation = newAggregation(operations);

//根据条件查询数据

AggregationResults results = mongoTemplate.aggregate(aggregation, "productInfo", ProductInfo.class);

//返回结果的map和分页信息和总记录数

return new PageImpl<>(results.getMappedResults(),pageable,totalCount);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值