查找多个日期时间对象mongodb之间的平均时间

时间:2018-05-03 11:13:56

标签: mongodb codeigniter

我想查找客户在特定位置访问的平均时间。我的数据结构如下:

{
  "_id": ObjectId("5aea9f9a83b391f80e00a1b1"),
  "Client": "ABC",
  "Timestamp": "2018-05-03 10:47:42"
 },
{
  "_id": ObjectId("5aea9f9a83b391f80e00c1a1"),
  "Client": "ABC",
  "Timestamp": "2018-05-03 11:05:04"
 }  ,
{
  "_id": ObjectId("5aea9f9a83b391f80e00a1e1"),
  "Client": "ABC",
  "Timestamp": "2018-05-03 13:05:04"
 }  ,
{
  "_id": ObjectId("5aea9f9a83b391f80eaea1e1"),
  "Client": "DEF",
  "Timestamp": "2018-05-03 11:20:44"
 }  

到目前为止,我刚刚在codeigniter中尝试了代码

$optn = array(
        array('$group' => array( '_id' => '$Client', 'date1' => array('$min' => '$Timestamp'), 'date2' => array('$max' => '$Timestamp'), 'avg_time' => array('$avg' =>  array('$subtract' => array('$date2', '$date1')))))
    );
$repeatdata = $this->mongo_db->aggregate("logsdata", $optn);
echo '<pre>';  print_r($repeatdata);

它给我结果:

 Array
    (
[waitedMS] => 0
[result] => Array
    (
        [0] => Array
            (
                [_id] => ABC
                [date1] => 2018-05-03 10:47:42
                [date2] => 2018-05-03 13:05:04
                [avg_time] => 
            )

        [1] => Array
            (
                [_id] => DEF
                [date1] => 2018-05-03 11:20:44
                [date2] => 2018-05-03 11:20:44
                [avg_time] => 
            )
     )
  )

0 个答案:

没有答案