【java】Spring+Mongodb,按月查询集合查询数据

北慕城南 提交于 2020-01-22 16:09:06

Spring+Mongodb在查询数据时,是可以指定查询集合的。那么现在的需求是按月份查询数据。

一、在serviceImpl上,先指定构造时间集合字符串

@Resource
private MongoTemplate mongoTemplate;

private String monthCollection = String.format("%s_warningInfo", formatDate(new Date(), "yyyyMM"));**
   

下面所有查询语句,指定集合时,都用的monthCollection

二、按id查询,指定集合

@Override
public WarningInfo loadById(String id) {
    WarningInfo warningInfo = mongoTemplate.findById(id, WarningInfo.class, monthCollection);
    return warningInfo;
    }

三、保存一条记录,指定集合

@Override
public int save(WarningInfo record) {
    record.setCreateTime(new Date());
    record.setUpdateTime(new Date());
    mongoTemplate.save(record, monthCollection);
    return 1;
}

四、删除一条记录,指定集合

@Override
public int delete(WarningInfo record) {
    mongoTemplate.remove(record, monthCollection);
    return 1;
}

五、分页查询,指定集合

@Override
public List<WarningInfo> findList(WarningInfo warningInfo, Integer pageindex, Integer pageSize, Map<String, Object> params) {
    // 提取字段
    BasicQuery query = new BasicQuery(new Document(), setFieldsForList());
    // 查询条件
    setFindCriterias(query, warningInfo, params);
    // 排序
    Sort sort = new Sort(Sort.Direction.DESC, "updateTime");
    // 分页
    Pageable pageable = PageRequest.of(pageindex - 1, pageSize, sort);
    // 指定集合查询
    List<WarningInfo> list = mongoTemplate.find(query.with(pageable), WarningInfo.class, monthCollection);
    
    return list;
}

六、查询数量,指定集合

@Override
public Integer loadCount(WarningInfo warningInfo, Map<String, Object> params) {
    Query query = new Query();
    // 查询条件
    setFindCriterias(query, warningInfo, params);
    
    Long count = mongoTemplate.count(query, WarningInfo.class, monthCollection);

    return count.intValue();
}

七、聚合查询,指定集合

/**
 * 查询子文档列表,使用聚合操作
 * @param   id 查询对象id
 * @param   pageIndex 分页查询的页码
 * @param   pageSize 每页的条数(默认:10)
 * @return  list<DBObject>
 */
@Override
public List<DBObject> findCommentsList(String id, Integer pageIndex, Integer pageSize) {
    //封装对象列表查询条件
    List<AggregationOperation> commonOperations = new ArrayList<>();
    //1. 指定查询主文档
    MatchOperation match = Aggregation.match(Criteria.where("id").is(id));
    commonOperations.add(match);
    // 2. 指定投影,返回哪些字段
    ProjectionOperation project = Aggregation.project("newsComments").andExclude("_id");
    commonOperations.add(project);
    //3. 拆分内嵌文档
    UnwindOperation unwind = Aggregation.unwind("newsComments");
    commonOperations.add(unwind);
    //4. 分页
    SkipOperation skip = Aggregation.skip((pageIndex - 1) * pageSize);
    commonOperations.add(skip);
    //5. 跳过
    LimitOperation limit = Aggregation.limit(pageSize);
    commonOperations.add(limit);
    //5. 排序
    SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "newsComments.commentTime");
    commonOperations.add(sort);
    //创建管道查询对象
	// Aggregation aggregation = Aggregation.newAggregation(commonOperations);
	
	// 指定集合聚合查询
    TypedAggregation<WarningInfo> aggregation = Aggregation.newAggregation(WarningInfo.class, commonOperations);
    
    AggregationResults<DBObject> reminds = mongoTemplate.aggregate(aggregation, monthCollection, DBObject.class);
    
    List<DBObject> mappedResults = reminds.getMappedResults();

    return mappedResults;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!