douyan4470 2013-09-17 20:42
浏览 46
已采纳

MongoDB,PHP每天获得独特的访问者

I'm creating some analytics script using PHP and MongoDB and I am a bit stuck. I would like to get the unique number of visitors per day within a certain time frame.

{
   "_id": ObjectId("523768039b7e7a1505000000"),
   "ipAddress": "127.0.0.1",
   "pageId": ObjectId("522f80f59b7e7a0f2b000000"),
   "uniqueVisitorId": "0445905a-4015-4b70-a8ef-b339ab7836f1",
   "recordedTime": ISODate("2013-09-16T20:20:19.0Z") 
}

The field to filter on is uniqueVisitorId and recordedTime.

I've created a database object in PHP that I initialise and it makes me a database connection when the object is constructed, then I have MongoDB php functions simply mapped to public function using the database connection created on object construction.

Anyhow, so far I get the number of visitors per day with:

public function GetUniqueVisitorsDiagram() {
    // MAP
    $map = new MongoCode('function() {
        day = new Date(Date.UTC(this.recordedTime.getFullYear(), this.recordedTime.getMonth(), this.recordedTime.getDate()));
        emit({day: day, uniqueVisitorId:this.uniqueVisitorId},{count:1});
    }');
    // REDUCE
    $reduce = new MongoCode("function(key, values) {
            var count = 0;
            values.forEach(function(v) {
                count += v['count'];
            });
            return {count: count};
        }");
    // STATS
    $stats = $this->database->Command(array(
        'mapreduce' => 'statistics',
        'map' => $map,
        'reduce' => $reduce,
        "query" => array(
            "recordedTime" =>
                array(
                    '$gte' => $this->startDate,
                    '$lte' => $this->endDate
                )
            ),
            "out" => array(
                "inline" => 1
            )
        ));
    return $stats;          
}

How would I filter this data correctly to get unique visitors? Or would it better to use aggregation, if so could you be so kind to help me out with a code snippet?

  • 写回答

2条回答 默认 最新

  • douyou9923 2013-09-17 21:35
    关注
        $m=new MongoClient();
        $db=$m->super_test;
        $db->gjgjgjg->insert(array(
            "ipAddress" => "127.0.0.1",
            "pageId" => new MongoId("522f80f59b7e7a0f2b000000"),
            "uniqueVisitorId" => "0445905a-4015-4b70-a8ef-b339ab7836f1",
            "recordedTime" => new MongoDate(strtotime("2013-09-16T20:20:19.0Z"))
        ));
        var_dump($db->gjgjgjg->find(array('recordedTime'=>array('$lte'=>new MongoDate(),'$gte'=>new MongoDate(strtotime('-1 week')))))->count()); // Prints 1
        $res=$db->gjgjgjg->aggregate(array(
                array('$match'=>array('recordedTime'=>array('$lte'=>new MongoDate(),'$gte'=>new MongoDate(strtotime('-1 week'))),'uniqueVisitorId'=>array('$ne'=>null))),
                array('$project'=>array('day'=>array('$dayOfMonth'=>'$recordedTime'),'month'=>array('$month'=>'$recordedTime'),'year'=>array('$year'=>'$recordedTime'))),
                array('$group'=>array('_id'=>array('day'=>'$day','month'=>'$month','year'=>'$year'), 'c'=>array('$sum'=>1)))
        )); 
        var_dump($res['result']);
    

    To answer the question entirely:

        $m=new MongoClient();
        $db=$m->super_test;
        $db->gjgjgjg->insert(array(
            "ipAddress" => "127.0.0.1",
            "pageId" => new MongoId("522f80f59b7e7a0f2b000000"),
            "uniqueVisitorId" => "0445905a-4015-4b70-a8ef-b339ab7836f1",
            "recordedTime" => new MongoDate(strtotime("2013-09-16T20:20:19.0Z"))
        ));
        var_dump($db->gjgjgjg->find(array('recordedTime'=>array('$lte'=>new MongoDate(),'$gte'=>new MongoDate(strtotime('-1 week')))))->count()); // Prints 1
        $res=$db->gjgjgjg->aggregate(array(
                array('$match'=>array('recordedTime'=>array('$lte'=>new MongoDate(),'$gte'=>new MongoDate(strtotime('-1 week'))),'uniqueVisitorId'=>array('$ne'=>null))),
                array('$project'=>array('day'=>array('$dayOfMonth'=>'$recordedTime'),'month'=>array('$month'=>'$recordedTime'),'year'=>array('$year'=>'$recordedTime'))),
                array('$group'=>array('_id'=>array('day'=>'$day','month'=>'$month','year'=>'$year','v'=>'$uniqueVisitorId'), 'c'=>array('$sum'=>1))),
                array('$group'=>array('_id'=>array('day'=>'$_id.day','month'=>'$_id.month','year'=>'$_id.year'),'c'=>array('$sum'=>1)))
        )); 
        var_dump($res['result']);
    

    Something close to that is what your looking for I believe.

    It will reutrn a set of documents that have the _id as the date and then the count of unique visitors for that day irresptive of the of the id, it simply detects only if the id is there.

    Since you want it per day you can actually exchange the dat parts for just one field of $dayOfYear I reckon.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题