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

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值