zoukankan      html  css  js  c++  java
  • java中mongo的条件查询

    @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);
        }
    

      

  • 相关阅读:
    iOS 索引列 使用详解
    iOS 搜索条使用详解
    iOS 百度地图使用详解
    在iOS中使用ZBar扫描二维码和条形码
    自学html-five(锚点、伪类、字符实体)
    自学html-four(css初始化及html语义标签 -> h标签 p标签 img标签 有序列表 无序列表 表格 超链接)
    自学html--htree(CSS)
    自学html--two(盒模型)
    自学html--one(div布局)
    常用控件补充(UIDatePicker、UIWebView)
  • 原文地址:https://www.cnblogs.com/amoyzhu/p/9023252.html
Copyright © 2011-2022 走看看