springdata-mongoTemplate对mongodb去重分页操作

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 orders = new ArrayList();  
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 details = new ArrayList();
AggregationResults outputType=mongoTemplate.aggregate(agg,TABLE_GROUP_LIST, BasicDBObject.class);  
for (Iterator 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去重分页操作)