mongodb

springdata-mongoTemplate对mongodb

2017-12-29  本文已影响0人  _Ryoma
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())
顺序不能颠倒

上一篇下一篇

猜你喜欢

热点阅读