【MongoDB】使用mongoTemplate查询某个时间段的数据

使用mongoTemplate来查询数据库中某一个时间段的数据时,有两种方式:
第一种:使用时间戳

	@RequestMapping(value="/api/v2/timeInterval/users",method=RequestMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "startTime", value = "开始时间戳,单位:ms", required = true, dataType = "Long", paramType = "query"),
		@ApiImplicitParam(name = "endTime", value = "结束时间戳,单位:ms", required = true, dataType = "Long", paramType = "query")})
	public ResponseEntity<?> getAllUserByUpdateTime(
			@RequestParam(name="startTime")Long startTime,
			@RequestParam(name="endTime")Long endTime){
		//  ...........其他代码忽略
		// long类型数据转化为时间戳
		Timestamp startTimestamp=new Timestamp(startTime);
		Timestamp endStTimestamp=new Timestamp(endTime);
		// 构建请求条件
		Criteria criteria = Criteria//
				.where("deletedStatus").is(Boolean.FALSE)//
				.andOperator(//
					new Criteria().and("updatedTimeStamp").gte(startTimestamp),
					new Criteria().and("updatedTimeStamp").lt(endStTimestamp));
		
	}

执行请求的Mongodb语句如下所示,时间已经被转化为了国标时间:

{ "deletedStatus" : false, "$and" : [ { "updatedTimeStamp" : { "$gte" : { "$date" : "2020-06-10T03:06:07.000Z"}}} , { "updatedTimeStamp" : { "$lt" : { "$date" : "2020-07-10T03:06:07.000Z"}}}] }

第二种:使用自定义格式的字符串语句,如:yyyy-MM-dd HH:mm:ss

	@RequestMapping(value="/api/v2/timeInterval/users",method=RequestMethod.GET)
	@ApiImplicitParams({
		@ApiImplicitParam(name = "startTime", value = "开始时间", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "endTime", value = "结束时间", required = true, dataType = "String", paramType = "query")})
	public ResponseEntity<?> getAllUserByUpdateTime(
			@RequestParam(name="startTime")String startTime,
			@RequestParam(name="endTime")String endTime){
		//  ...........其他代码忽略
		DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime startLocalDateTime=LocalDateTime.parse(startTime, dateTimeFormatter);
		LocalDateTime endLocalDateTime=LocalDateTime.parse(endTime, dateTimeFormatter);
		// 构建请求条件
		Criteria criteria = Criteria//
				.where("deletedStatus").is(Boolean.FALSE)//
				.andOperator(//
					new Criteria().and("updatedTimeStamp").gte(startLocalDateTime),
					new Criteria().and("updatedTimeStamp").lt(endLocalDateTime));
		
	}

执行请求的Mongodb语句如下所示:

{ "deletedStatus" : false, "$and" : [ { "updatedTimeStamp" : { "$gte" : { "$date" : "1591758367000"}}} , { "updatedTimeStamp" : { "$lt" : { "$date" : "1594350367000"}}}] }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
使用MongoTemplate进行聚合查询来获取数据总数,您可以使用Aggregation类和AggregationOperation接口来构建聚合查询。以下是使用MongoTemplate查询名为attendance的集合中数据总数的示例代码: ```java import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.aggregation.CountOperation; import org.springframework.data.mongodb.core.query.Criteria; public class Main { private final MongoTemplate mongoTemplate; public Main(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } public long getAttendanceCount() { CountOperation countOperation = Aggregation.count().as("count"); Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("collectionName").is("attendance")), countOperation ); AggregationResults<CountResult> results = mongoTemplate.aggregate(aggregation, "system.namespaces", CountResult.class); CountResult countResult = results.getUniqueMappedResult(); if (countResult != null) { return countResult.getCount(); } else { return 0; } } public static void main(String[] args) { // 创建MongoTemplate实例 // MongoTemplate mongoTemplate = ...; Main main = new Main(mongoTemplate); long count = main.getAttendanceCount(); System.out.println("Attendance count: " + count); } private static class CountResult { private long count; public long getCount() { return count; } public void setCount(long count) { this.count = count; } } } ``` 请确保已经创建了MongoTemplate实例,并将其传递给Main类的构造函数。然后,可以调用getAttendanceCount方法来执行聚合查询并获取名为attendance的集合中的数据总数。聚合查询通过匹配条件来筛选出名为attendance的集合,然后使用count操作来计算文档数量。最后,从聚合结果中提取计数值并返回。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值