美文网首页mongodb
springdata-mongoTemplate对mongodb

springdata-mongoTemplate对mongodb

作者: _Ryoma | 来源:发表于2017-12-29 17:05 被阅读0次
    import org.springframework.data.mongodb.core.aggregation.Aggregation
    import org.springframework.data.mongodb.core.mongoTemplate
     
    Criteria criteria = new Criteria();  
    criteria.and("groupid").in(bean.getGroupId());
    List<Order> orders = new ArrayList<Order>();  
    orders.add(new Order(Direction.ASC, "_id"));  
    Sort sort = new Sort(orders);  
    Aggregation agg = Aggregation.newAggregation(  
        Aggregation.match(criteria),  //查询条件
        Aggregation.group("detail"),
        Aggregation.sort(sort),
        Aggregation.skip((fianlPageNum - 1) * bean.getPageSize()),//跳到第几个开始
        Aggregation.limit(bean.getPageSize())//查出多少个数据
    );  
    
    List<String> details = new ArrayList<String>();
    AggregationResults<BasicDBObject> outputType=mongoTemplate.aggregate(agg,TABLE_GROUP_LIST, BasicDBObject.class);  
    for (Iterator<BasicDBObject> iterator = outputType.iterator(); iterator.hasNext();) {  
    DBObject obj =iterator.next(); 
    details.add((String) obj.get("_id"));
    } 
    注意:
        Aggregation.skip((fianlPageNum - 1) * bean.getPageSize()),
        Aggregation.limit(bean.getPageSize())
    顺序不能颠倒
    
    

    相关文章

      网友评论

        本文标题:springdata-mongoTemplate对mongodb

        本文链接:https://www.haomeiwen.com/subject/qnfwgxtx.html