mongodb 分组分页

public List getcommunity(String useraccount,int pageNo,int pageSize,int status)throws Exception{
            Listresult=new ArrayList();
            MongoTemplate temp =  ContextUtils.getBean("bdtestMongoTemplate");

            // -- 模板
            // -- 过滤 条件
            Criteria where = Criteria.where("status").is(status);
            where.andOperator(Criteria.where("is_xo").ne(1));
            where.and("uploadUserAccount").is(useraccount); 
            
            
            MatchOperation match = Aggregation.match(where);
            // -- 分组聚合
            //GroupOperation group = Aggregation.group("status","communityName", "communityId","uploadUserName","uploadUserAccount").count().as("num");
            GroupOperation group = Aggregation.group("communityName", "communityId");
            // --分页
            LimitOperation limit = Aggregation.limit(pageSize);
            SkipOperation skip = Aggregation.skip((pageNo-1)*pageSize);
            
            // -- 集成选项
            Aggregation aggregation = Aggregation.newAggregation(
                    match, group,skip,limit
            );
            
            
            /*System.out.println(aggregation.toString());*/
            // -- 执行和并操作 如果 有 对应的结果Bean 可以把 BasicDBObject.class 替换掉
            AggregationResults outputType = temp.aggregate(aggregation
                    , "pictureInfo", BasicDBObject.class);

            Iterator iterator = outputType.iterator();
            
            //-- 查询
            while (iterator.hasNext()) {
                BasicDBObject obj= iterator.next();
                Map result_map=new HashMap<>();
                
                boolean idflag=obj.containsKey("communityId");
                boolean nameflag=obj.containsKey("communityName");
                if(idflag&&nameflag){
                    result_map.put("communityId", obj.getString("communityId").toString());
                    result_map.put("communityName", obj.getString("communityName").toString());
                    result.add(result_map);
                }
                
            }
            
            return result;
        }

转载于:https://my.oschina.net/zzp123456/blog/1588217

你可能感兴趣的:(mongodb 分组分页)