mongodb-分组分页

1, 添加测试数据

@Test
    public  void save() {
        News n = null;
        for (int i = 0; i < 10000; i++) {
            n = new News();
            n.setTitle("title_" + i);
            n.setUrl("url_" + i);
            
            //2014-01-01到2014-01-01之间的随机时间
            Date randomDate=DateUtil.randomDate("2014-01-01","2014-05-11");
            //MongoDB里如果时间类型存的是Date,那么会差8个小时的时区,因为MongoDB使用的格林威治时间,中国所处的是+8区,so……
            //比如我保存的是2014-05-01 00:00:00,那么保存到MongoDB里则是2014-05-01 08:00:00,所以为了统一方面,那就保存字符串类型,底下保存的long类型
            n.setPublishTimeStr(DateUtil.formatDateTimeByDate(randomDate));
            //long类型在查询速度中肯定会比较快
            n.setPublishTime(randomDate.getTime());
            n.setPublishDate(randomDate);
            
            n.setPublishMedia("publishMedia_" + i);
            
            String[] areaArr = {"1024", "102401", "102402", "102403", "102404", "102405", "102406", "102407", "102408"
                    , "10240101", "10240102", "10240201", "10240202", "10240301", "10240302", "10240401", "10240402"
                    , "10240501", "10240502", "10240601", "10240602", "10240701", "10240702", "10240801", "10240802"};
            int areaNum=(int)(Math.random() * areaArr.length);//产生0-strs.length的整数随机数
            String area = areaArr[areaNum];
            n.setArea(area);
            
            String[] ckeyArr = {"A101", "A102", "A201", "A202", "A203"
                    , "B101", "B102", "B103", "C201", "C202", "C203", "22", "23", "24", "25", "26"};
            int ckeyNum=(int)(Math.random() * ckeyArr.length);//产生0-strs.length的整数随机数
            List<String> list = new ArrayList<String>();
            for (int j = 0; j < ckeyNum; j ++) {
                int ckeyNum1=(int)(Math.random() * ckeyArr.length);//产生0-strs.length的整数随机数
                list.add(ckeyArr[ckeyNum1]);
            }
            n.setClassKey(list);
            
            Integer[] evalArr = {1, 0};
            int evalNum=(int)(Math.random() * evalArr.length);//产生0-strs.length的整数随机数
            n.setEvaluate(evalArr[evalNum]);
            
            Integer[] mproArr = {1, 2, 100};
            int mproNum=(int)(Math.random() * mproArr.length);//产生0-strs.length的整数随机数
            n.setMediaProperty(mproArr[mproNum]);

            Integer[] mtypeArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
            int mtypeNum=(int)(Math.random() * mtypeArr.length);//产生0-strs.length的整数随机数
            n.setMediaType(mtypeArr[mtypeNum]);
            
            Integer[] levelArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
            int levelNum=(int)(Math.random() * levelArr.length);//产生0-strs.length的整数随机数
            n.setLevel(levelArr[levelNum]);
            
            newsService.save(n);
        }
        System.out.println("OK");
    }

2, 使用 dbCollection进行分页:

/**
     * 
     * 功能:使用Mongo本身提供的AggregationOutput进行分组查询
     * 参数:   
     * 创建人:OnTheRoad_Lee 
     * 修改人:OnTheRoad_Lee 
     * 最后修改时间:2014-5-26
     */
    public void testGroup1 () {
        //按照eval字段进行分组,注意$eval必须是存在mongodb里面的字段,不能写$evaluate(此字段是News类中定义的,和存入mongodb中的有区别)
        //{$group:{_id:{'AAA':'$BBB'},CCC:{$sum:1}}}固定格式:把要分组的字段放在_id:{}里面,BBB是mongodb里面的某个字段,AAA是BBB的重命名,CCC是$sum:1的重命名
        //此查询语句== select eval as eval, count(*) as docsNum from news group by eval having docsNum>=85 order by docsNum desc
        //具体的mongodb和sql的对照可以参考:http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
        String groupStr = "{$group:{_id:{'eval':'$eval'},docsNum:{$sum:1}}}";
        DBObject group = (DBObject) JSON.parse(groupStr);
        String matchStr = "{$match:{docsNum:{$gte:85}}}";
        DBObject match = (DBObject) JSON.parse(matchStr);
        String sortStr = "{$sort:{_id.docsNum:-1}}";
        DBObject sort = (DBObject) JSON.parse(sortStr);
        AggregationOutput output = mongoTemplate.getCollection("news").aggregate(group, match, sort);
        System.out.println(output.getCommand());
        //转换为执行原生的mongodb查询语句
        //{ "aggregate" : "news" , "pipeline" : [ { "$group" : { "_id" : { "eval" : "$eval"} , "docsNum" : { "$sum" : 1}}} , { "$match" : { "docsNum" : { "$gte" : 85}}} , { "$sort" : { "_id.docsNum" : -1}}]}
        System.out.println(output.getCommandResult());
        //查询结果
        //{ "serverUsed" : "localhost/127.0.0.1:47017" , "result" : [ { "_id" : { "evaluate" : 1} , "docsNum" : 9955} , { "_id" : { "evaluate" : 0} , "docsNum" : 10047}] , "ok" : 1.0}
        
        //也可以把查询结果封装到NewsNumDTO,这样以一个dto对象返回前台操作就更容易了
        NewsNumDTO dto = new NewsNumDTO();
        for( Iterator< DBObject > it = output.results().iterator(); it.hasNext(); ){
            BasicDBObject dbo = ( BasicDBObject ) it.next();
            BasicDBObject keyValus = (BasicDBObject)dbo.get("_id");
            int eval = keyValus.getInt("eval");
            long docsNum = ((Integer)dbo.get("docsNum")).longValue();
            if(eval == 1){
                dto.setPositiveNum(docsNum);
            }else {
                dto.setNegativeNum(docsNum);
            }
        }
        
    }

3, 使用mongotemplate 进行分组分页

    /**
     * 按日期排序显示错误的poi信息
     */
    @Override
    public PageEntity<GroupEntity> getFaildPoisByPage(Long page, Integer pageSize) {

        Criteria criteria = Criteria.where("status").is(1);
        Aggregation aggregationall = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group("task_ids").count().as("totalCount"),
                Aggregation.sort(Sort.Direction.DESC, "totalCount"));
        AggregationResults<GroupEntity> aggResall = mongoTemplate.aggregate(aggregationall, "scrapy_error_pois",
                GroupEntity.class);
        List<GroupEntity> listResall = aggResall.getMappedResults();
        long totalCount = listResall.size();
        System.out.println(totalCount);

        PageEntity<GroupEntity> pageEntity = new PageEntity<>();
        pageEntity.setPage(page);
        pageEntity.setPageSize(pageSize);
        pageEntity.setTotalCount(totalCount);
        @SuppressWarnings("deprecation")
        Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),
                Aggregation.group("task_ids").count().as("totalCount"),
                Aggregation.sort(Sort.Direction.DESC, "totalCount"), Aggregation.skip(pageEntity.getStart().intValue()),
                Aggregation.limit(pageSize));
        // Aggregation.skip(1), Aggregation.limit(3));
        // System.out.println("--mongoDBSQL--" + aggregation.toString());
        AggregationResults<GroupEntity> aggRes = mongoTemplate.aggregate(aggregation, "scrapy_error_pois",
                GroupEntity.class);
        // BasicDBList bdbl =(BasicDBList) aggRes.getRawResults().get("result");
        List<GroupEntity> listRes = aggRes.getMappedResults();
        for (GroupEntity error : listRes) {
            ScrapyJob scrapyJob = mongoTemplate.findOne(
                    Query.query(Criteria.where("_id").is(error.get_id().split(", *")[0])), ScrapyJob.class);
//            error.setTask_one_id(scrapyJob.getTask_name());
            error.setTask_one_id(scrapyJob == null ? DateUtils.getDate() : scrapyJob.getTask_name());
            
            // System.out.println(error.get_id());
        }
        pageEntity.setRows(listRes);
        return pageEntity;
    }

 

 

原地址: http://www.cnblogs.com/ontheroad_lee/p/3756247.html

http://ask.csdn.net/questions/237974

posted @ 2017-06-06 11:19  bronk  阅读(2528)  评论(0编辑  收藏  举报