首页  编辑  

Mongodb Group by limit(分组查询和分页)功能

Tags: /Java/   Date Created:
Mongodb中,实现类似以下查询的功能(分组和分页):
select distinct groupid, hcpID 
from tableA
group by groupId
limit 0, 10
代码:
  1.   @Override
  2.   public Page<Demo> findHistoryPageList(Integer page, Integer size, String groupId, Long hcpId) {
  3.     Criteria criteria = getCriteria(groupId);
  4.     if (hcpId != null) {
  5.       criteria.and(HCP_ID).is(hcpId);
  6.     }
  7.     Aggregation aggregation = Aggregation.newAggregation(
  8.         CdccMedicalGroupInfo.class,
  9.         Aggregation.match(criteria),
  10.         Aggregation.group(GROUP_ID).first(GROUP_ID).as(GROUP_ID).first(HCP_ID).as(HCP_ID)
  11.     );
  12.     AggregationResults<Demo> results = mongoTemplate.aggregate(aggregation, Demo.classDemo.class);
  13.     List<Demo> groupIds = results.getMappedResults();
  14.     int formIndex = page * size;
  15.     int toIndex = Math.min((page + 1) * size, groupIds.size());
  16.     if (formIndex > toIndex){
  17.       formIndex = 0;
  18.       toIndex = 0;
  19.     }
  20.     return new PageImpl<>(groupIds.subList(formIndex, toIndex), PageRequest.of(page, size), groupIds.size());
  21.   }

如果要实现以下功能:
select count(*) as total from (
select groupid from
from tableA
group by groupId
    )
代码如下:
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.data.mongodb.core.MongoTemplate;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.data.mongodb.core.aggregation.Aggregation;
  5. import org.springframework.data.mongodb.core.aggregation.AggregationResults;
  6. import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
  7. @Service
  8. public class TableAService {
  9.     private final MongoTemplate mongoTemplate;
  10.     @Autowired
  11.     public TableAService(MongoTemplate mongoTemplate) {
  12.         this.mongoTemplate = mongoTemplate;
  13.     }
  14.     public long getCountDistinctGroupId() {
  15.         Aggregation aggregation = newAggregation(
  16.             group("groupId"),
  17.             group().count().as("count")
  18.         );
  19.         AggregationResults<CountResult> results = mongoTemplate.aggregate(
  20.             aggregation, "tableA", CountResult.class
  21.         );
  22.         CountResult countResult = results.getUniqueMappedResult();
  23.         if (countResult != null) {
  24.             return countResult.getCount();
  25.         } else {
  26.             return 0L;
  27.         }
  28.     }
  29. }
  30. class CountResult {
  31.     private long count;
  32.     public long getCount() {
  33.         return count;
  34.     }
  35.     public void setCount(long count) {
  36.         this.count = count;
  37.     }
  38. }
不使用实体类的写法:
  1. List<Map> totalResult = mongoTemplate.aggregate(
  2.     Aggregation.newAggregation(
  3.         Aggregation.match(criteria),
  4.         Aggregation.group("groupID"),
  5.         Aggregation.group("groupID").count().as("total")
  6.     ),
  7.     "TableA",
  8.     Map.class
  9. ).getMappedResults();
  10. long total = Long.valueOf(totalResult.get(0).get("total").toString());