我的nodejs应用程序中有一个mongo聚合查询,这里是代码。
var tz = params.tz || util.config.TZ.default_timezone;
var fromDate = moment(new Date(params.from)).tz(tz).format();
var fromTimeOffset = moment.parseZone(fromDate).zone();
fromTimeOffset = fromTimeOffset * 60 *1000;
var adjustTime = (-1) * fromTimeOffset ;
var match={"$match":{"t":{"$gte":params.from.toISOString(), "$lte":params.to.toISOString()},"type":"content_views"}};
r.inject_match(match, req_query.where);
var dashboard_query = [match, {$project: { kayp_views_cnt: 1, date: { $add: ["$t", adjustTime ] } }},
{$group: { _id: { year: { $year: [ {'$add': ['$date', 0]} ]},
month: { $month: [ {'$add': ['$date', 0]} ]},
day: { $dayOfMonth: [ {'$add': ['$date', 0 ]} ]}},
name: {'$first': '$date'},
count: {'$sum': 1} }},
{'$sort': {name: 1}},
{$project: { _id: 0, count: 1, day_t: { $dayOfMonth: '$name'}, month_t: { $month: '$name'}, year_t: { $year: '$name'}}}] ;
这里我试图调整时间,mongo集合中的“$ t”列是字符串格式的日期。所以我想将其转换为日期日期类型,以便我可以添加调整后的时间。
这是样本集合数据:
{
"_id" : "29",
"timestamp" : 1602700200006,
"t" : "2020-10-25T04:30:00.00Z",
"user_id" : "1",
"user_name" : "Manu"
}