我的插入数据是
{ "_id" : ObjectId("5992e6f52479d56b0b2709bf"),
"TIMESTAMP" : ISODate("2017-08-15T12:19:25.680+0000"),
"RECORD" : 20507361.0,
"BattV_Min" : 11.9,
"SEVolt" : 0.0
}
我的获取日期的查询是
$collection1 = $db->tablename;
$filter1 = array('timestamp' => array('$gte' => $acst1, '$lte' => $acst2));
$options1 = array('projection' => array('_id' => 0,'temperature' => 1,timestamp => 1));
$oresult = $collection1->find($filter1,$options1);
我正在获取数十亿的数据,所以我想使用group by方法优化我的查询,就像我有70000以下的数据一样,如果我的数据范围在70000到100000之间,那么我将按分组进行分组,按时间和最后一天。
答案 0 :(得分:0)
你可以使用聚合函数在mongodb中使用group by而不是日期格式的项目,如果你想要列值也比使用$ addToSet函数和项目那个平均值使用avg函数。
$collection1 = $db->tablename;
$oresult = $collection1->aggregate([
[
'$match' => array(TIMESTAMP => array('$gte' => $acst1, '$lte' => $acst2))
],
[
'$project' => [
'year' => ['$year' =>'TIMESTAMP'],
'month' => ['$month' => '$TIMESTAMP'],
'day' => ['$dayOfMonth' => '$TIMESTAMP'],
'hour' => ['$hour' => '$TIMESTAMP'],
'minute' => ['$minute' => '$TIMESTAMP'],
'second' => ['$second' => '$TIMESTAMP'],
'millisecond' => ['$millisecond' => '$TIMESTAMP'],
'value' => '$BattV'
]
],
[
'$group' => [
'_id' => [
'year' => '$year',
'month' => '$month',
'day' => '$day',
'hour' => '$hour',
'minute' => '$minute',
'second' => '$second',
'millisecond' => '$millisecond'
],
'count' => ['$sum' => 1],
'values' => ['$addToSet' => '$value']
]
],
[
'$sort' => ['_id' => 1]
],
[
'$project' => [
'a' => '$count',
'new' => ['$avg' => '$values'],
'y' => [
'$concat' => [
['$substr' => ['$_id.year', 0, 4]],
'-',
[
'$cond' => [
['$lte' => ['$_id.month', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.month', 0, 2]],
]
],
['$substr' => ['$_id.month', 0, 2]]
]
],
'-',
[
'$cond' => [
['$lte' => ['$_id.day', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.day', 0, 2]],
]
],
['$substr' => ['$_id.day', 0, 2]]
]
],
' ',
[
'$cond' => [
['$lte' => ['$_id.hour', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.hour', 0, 2]],
]
],
['$substr' => ['$_id.hour', 0, 2]]
]
],
':',
[
'$cond' => [
['$lte' => ['$_id.minute', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.minute', 0, 2]],
]
],
['$substr' => ['$_id.minute', 0, 2]]
]
],
':',
[
'$cond' => [
['$lte' => ['$_id.second', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.second', 0, 2]],
]
],
['$substr' => ['$_id.second', 0, 2]]
]
],
'.',
[
'$cond' => [
['$lte' => ['$_id.millisecond', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.millisecond', 0, 1]],
]
],
['$substr' => ['$_id.millisecond', 0, 1]]
]
]
]
],
]
]
]
);