java mongo 根据时间戳聚合查询某一天的数据

时间戳聚合查询某一天的数据

 {
   
    "_id": {
   "$oid": "5ff83089c3db080017fdc968"},
    "app_id": 96,
    "timestamp": 1610100873416
  },
  {
   
    "_id": {
   "$oid": "5ff8303ac3db080017fdc966"}, 
    "app_id": 96,
    "timestamp": 1610100794510
  }
聚合查询SQL:

比如我想查询的SQL是:

select  day ,count(*) from xxx where app_id =xxx group by app_id , day

在mongo中将时间戳转化为时间的SQL如下:

mongo将时间戳转为日期格式
db.xxx.aggregate(
   [
     {
   
       $project: {
   
          timestamp: 1,
          app_id: 1,
          day: {
   $dateToString: {
   format: "%Y-%m-%d %H:%M:%S:%L", date:{
   "$add":[new Date(0),"$timestamp"]}}},
          day8: {
   $dateToString: {
   format: "%Y-%m-%d %H:%M:%S:%L", date:{
   "$add":[new Date(0),"$timestamp",28800000]}}}
       }
     }
   ]
)

转换的结果如下:day8是多个8个小时的结果

  {
   
    "_id": {
   "$oid": "5fb12ea4c33c7400175b9cb5"},
    "app_id": 45,
    "day": "2020-10-23 10:02:12:461",
    "day8": "2020-10-23 18:02:12:461",
    "timestamp": 1603447332461
  },
  {
   
    "_id": {
   "$oid": "5fb12ea4c33c7400175b9cb6"},
    "app_id": 45,
    "day": "2020-10-23 10:02:12:503",
    "day8": "2020-10-23 18:02:12:503",
    "timestamp": 1603447332503
  }
match和project组合查询
db.xxxx.aggregate(
    {
   $match:
         {
     "app_id":96,
            "timestamp": {
   "$exists": true},
            "timestamp": {
    "$gte": 1610035200000,"$lte" :1610121600000}
         }
     },
     {
   
       $project: {
   
          timestamp: 1,
         	app_id: 1,
          day: {
   $dateToString: {
   format: "%Y-%m-%d %H:%M:%S:%L", date:{
   "$add":[new Date(0),"$timestamp"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用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操作来计算文档数量。最后,从聚合结果中提取计数值并返回。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值