java中mongo的條件查詢

來源:互聯網
上載者:User

標籤: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的條件查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.