標籤:return status count imp gen ges alc stat product
@Override public Page<ProductInfo> findAll(Pageable pageable, ProductInfo productInfo) {//建立一個操作彙總operations List<AggregationOperation> 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<ProductInfo> 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<ProductInfo> results = mongoTemplate.aggregate(aggregation, "productInfo", ProductInfo.class);//返回結果的map和分頁資訊和總記錄數 return new PageImpl<>(results.getMappedResults(),pageable,totalCount); }
java中mongo的條件查詢