mongoDB Java的aggregate查询结果的遍历不停向mongoDB发送命令

不断重复的日志为:
2016-01-28 10:41:24,284   http-bio-8088-exec-8 DEBUG- Sending command {aggregate : BsonString{value='ad_batch'}} to database kzhuoAd on connection [connectionId{localValue:12, serverValue:2269}] to server 10.0.0.241:27017
2016-01-28 10:41:24,284   http-bio-8088-exec-3 DEBUG- Sending command {aggregate : BsonString{value='ad_batch'}} to database kzhuoAd on connection [connectionId{localValue:3, serverValue:2259}] to server 10.0.0.241:27017
2016-01-28 10:41:24,284   http-bio-8088-exec-5 DEBUG- Sending command {aggregate : BsonString{value='ad_batch'}} to database kzhuoAd on connection [connectionId{localValue:6, serverValue:2262}] to server 10.0.0.241:27017
2016-01-28 10:41:24,284   http-bio-8088-exec-23 DEBUG- Sending command {aggregate : BsonString{value='ad_batch'}} to database kzhuoAd on connection [connectionId{localValue:8, serverValue:2264}] to server 10.0.0.241:27017
2016-01-28 10:41:24,285   http-bio-8088-exec-4 DEBUG- Command execution completed

原因:
错误写法:
        AggregateIterable iterableshow=MongodbDao.queryByAggregate("kzhuoAd", "ad_batch", clickList);
        int clickCount=0;
        while(iterableshow.iterator().hasNext()){//这样写是有问题的会不停的向mongoDB请求命令,并且每次next都是同一个
                                                                           //       Document
            Document document=iterableshow.iterator().next();
            clickCount=clickCount+document.getInteger("count");
        }

正确写法:
        AggregateIterable iterableshow=MongodbDao.queryByAggregate("kzhuoAd", "ad_batch", showList);
        MongoCursor iterator=iterableshow.iterator();
        int showCount=0;
        while(iterator.hasNext()){
            Document document=iterator.next();
            showCount=showCount+document.getInteger("count");
        }

你可能感兴趣的:(mongodb)