Mongodb aggregate timezone 问题
来源:互联网 发布:北京海洋馆客流量数据 编辑:程序博客网 时间:2024/05/19 03:46
在用aggregate 进行数据统计处理的时候,由于系统默认使用Mongodb的UTC时间,与我们时区差了8小时,会出现结果误差。为了获得正确的结果,在进行
aggregate 处理时需要在原来的基础上做加8小时处理。
测试数据如下
下面我们来计算下shop_id 等于57300412且时间大于’2014-04-01T00:02:00Z’ 按天排序的统计结果。shell表达式如下
1
2
3
4
5
6
7
8
9
10
11
12
13
//经过时差处理
db.test.aggregate({$match:{
'_id.shop_id'
:
57300412
}},
{$group:{
_id:{
year:{$year:{$add:[
'$_id.date'
,
28800000
]}},
month:{$month:{$add:[
'$_id.date'
,
28800000
]}},
day:{$dayOfMonth:{$add:[
'$_id.date'
,
28800000
]}
}},
amount:{$sum:
'$value.amount'
},
count:{$sum:
'$value.count'
},
cost:{$sum:
'$value.cost'
}}},
{$project:{_id:
1
,count:
1
,amount:
1
,cost:
1
}}
)
1
2
3
4
5
6
7
8
9
10
11
12
13
//没有经过时差处理
db.test.aggregate({$match:{
'_id.shop_id'
:
57300412
}},
{$group:{
_id:{
year:{$year:
'$_id.date'
},
month:{$month:
'$_id.date'
},
day:{$dayOfMonth:
'$_id.date'
}
},
amount:{$sum:
'$value.amount'
},
count:{$sum:
'$value.count'
},
cost:{$sum:
'$value.cost'
}}},
{$project:{_id:
1
,count:
1
,amount:
1
,cost:
1
}}
)
对比结果如下
注意上图增加时差和未增加时差的结果是不一样的。
说明:
1
2
$dayOfYear:计算日期的该年第几天,返回
1
到
366
$add:[
'$_id.date'
,
28800000
]: 时区数据校准,
8
小时换算成毫秒数为
8
*
60
*
60
*
1000
=
28800000
.
相应的Java代码片段如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
DBObject match = new BasicDBObject(
"$match"
, new BasicDBObject(
"_id.shop_id"
, Integer.parseInt(shopAmount.getShopId())).
append(
"_id.date"
, new BasicDBObject(
"$gte"
, startDate).append(
"$lte"
, endDate)));
BasicDBList dbList = new BasicDBList();
dbList.add(
"$_id.date"
);
dbList.add(
28800000
);//解决timezone
8
小时时差
Map<String, Object> dbObjIdMap = new HashMap<String, Object>();
dbObjIdMap.put(
"year"
, new BasicDBObject(
"$year"
,new BasicDBObject(
"$add"
,dbList)));
dbObjIdMap.put(
"month"
, new BasicDBObject(
"$month"
,new BasicDBObject(
"$add"
,dbList)));
dbObjIdMap.put(
"dayOfMonth"
, new BasicDBObject(
"$dayOfMonth"
,new BasicDBObject(
"$add"
,dbList)));
DBObject groupFields = new BasicDBObject(
"_id"
, new BasicDBObject(dbObjIdMap));
groupFields.put(
"amount"
, new BasicDBObject(
"$sum"
,
"$value.amount"
));
groupFields.put(
"count"
, new BasicDBObject(
"$sum"
,
"$value.count"
));
groupFields.put(
"cost"
, new BasicDBObject(
"$sum"
,
"$value.cost"
));
DBObject group = new BasicDBObject(
"$group"
, groupFields);
DBObject fields = new BasicDBObject(
"_id"
,
1
);
fields.put(
"cost"
,
1
);
fields.put(
"amount"
,
1
);
fields.put(
"count"
,
1
);
DBObject project = new BasicDBObject(
"$project"
, fields );
DBObject sort = new BasicDBObject(
"$sort"
, new BasicDBObject(
"_id"
,
-1
));
/* 查看Group结果 */
AggregationOutput output = collection.aggregate(match, group,project,sort); // 执行 aggregation命令
System.out.println(output.getCommandResult());
Collection<Object> c = output.getCommandResult().values();
Object o[] = c.toArray();
BasicDBList resultList = (BasicDBList) o[
1
];
0 0
- Mongodb aggregate timezone 问题
- Date, TimeZone, MongoDB, java中date的时区问题
- Mongodb Aggregate,MapReduce Sample
- mongodb aggregate 级联查询
- mongodb aggregate 聚合操作
- mongodb的aggregate 用法
- java mongodb aggregate用法
- Mongodb——Aggregate
- MongoDB aggregate聚合
- MongoDB aggregate 聚合管道
- mongodb 聚合aggregate 乘法
- MongoDB中的聚合 aggregate
- java mongodb aggregate用法
- golang mongodb Aggregate
- MongoDB aggregate() 聚合
- java mongodb aggregate用法
- MongoDB | 聚合 aggregate
- MongoDB的聚合(aggregate)
- UIBezierPath精讲
- 尽情享受美妙音乐----英国PURE公司Jongo S3无线音箱一周使用体验
- java 修改web-root folder 的值
- weChat开发
- New Year and Days
- Mongodb aggregate timezone 问题
- 错误:HttpServlet was not found on the Java
- Eclipse开发工具的使用
- 根据地址获取经度纬度
- MySQL数据库的安装
- NRT日志系统:flume+morphline+solr+hue配置
- 关于java的几个名词
- SQL中的cast和convert的用法和区别
- monkeytalk 启动、关闭 android应用程序 解决方法