萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 使用php mongodb擴展時比較需要注意的事項

使用php mongodb擴展時比較需要注意的事項

最近在使用php的mongo 擴展進行數據統計計算,其中有一個時間戳字段,由於精確到了毫秒,長度有13位,但由於開始的時候是以字符串的形式存儲:  代碼如下 復制代碼

 


{ "_id" : ObjectId("504eea97e4b023cf38e34039"), "in_ts" : NumberLong("1347349143699"), "log" : { "guid" : "4D1F3079-7507-F4B0-E7AF-5432D5D8229D", "p" : "View_Prop_YepPage_Zheng", "cid" : "11", "url" : "http://shanghai.haozu.com/rental/broker/n/10481780", "rfpn" : "Listing_V2_IndexPage_All", "site" : "haozu", "agent" : "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)", "stamp" : "1347349162159", "cip" : "116.226.70.44", "referer" : "http://shanghai.haozu.com/shop/1464934/", "cstamp" : "1347349323125", "sessid" : "FA798056-F9E7-F961-41E0-CC95C850FA47", "uguid" : "C00FF55B-3D3D-4B31-4318-12345B0DBE64", "pn" : "View_Prop_YepPage_Zheng", "cstparam" : { "proId" : NumberLong(10481780), "brokerId" : "326792", "tradeType" : "2", "userType" : "0", "channel" : "site", "entry" : "1", "COMMID" : "1666" } }, "out_ts" : NumberLong("1347349466083"), "rule" : 0, "status" : "ok", "txid" : 0 }

後來改成數字格式:

 

 代碼如下 復制代碼

{ "_id" : ObjectId("504eea97e4b023cf38e34039"), "in_ts" : NumberLong("1347349143699"), "log" : { "guid" : "4D1F3079-7507-F4B0-E7AF-5432D5D8229D", "p" : "View_Prop_YepPage_Zheng", "cid" : "11", "url" : "http://shanghai.haozu.com/rental/broker/n/10481780", "rfpn" : "Listing_V2_IndexPage_All", "site" : "haozu", "agent" : "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)", "stamp" : NumberLong("1347349162159"), "cip" : "116.226.70.44", "referer" : "http://shanghai.haozu.com/shop/1464934/", "cstamp" : "1347349323125", "sessid" : "FA798056-F9E7-F961-41E0-CC95C850FA47", "uguid" : "C00FF55B-3D3D-4B31-4318-12345B0DBE64", "pn" : "View_Prop_YepPage_Zheng", "cstparam" : { "proId" : NumberLong(10481780), "brokerId" : "326792", "tradeType" : "2", "userType" : "0", "channel" : "site", "entry" : "1", "COMMID" : "1666" } }, "out_ts" : NumberLong("1347349466083"), "rule" : 0, "status" : "ok", "txid" : 0 }為字符串時,使用下面的查詢是正常的

        $query = array ('log.stamp' => array ('$gte' => ‘1347346800000’, '$lt' => ‘1347350400000’));

但是改為數字後,使用下面的查詢,死活沒有結果,但是直接在mongo客戶端直接查詢是有結果的:

 

 代碼如下 復制代碼  db.haozu_success.find({'log.stamp':{$gte:1347346800000,$lt:1347350400000}})

php手冊上也是這麼個用法:

 代碼如下 復制代碼         $query = array ('log.stamp' => array ('$gte' => 1347346800000, '$lt' => 1347350400000));

花了好大一會找原因,開始時懷疑是php擴展的bug導致,經過一番思考。突然想到可能是類型問題導致,發現手冊上有Types 介紹,所以正確的用法如下:

 代碼如下 復制代碼

        $query = array ('log.stamp' => array ('$gte' => new MongoInt64($time_range['start']), '$lt' => new MongoInt64($time_range['end'])));

另外,在使用mapreduce進行數據統計時,為了防止cursor出現超時異常,還需要設置一下超時時間

 

 代碼如下 復制代碼 $map = new MongoCode ( '
                function(){
                    var prop_id=this.log.cstparam.proId;
                    var key=this.log.site+prop_id
                    emit(key,{"channel":this.log.site,"prop_id":prop_id,"count":1});
                }
                ' );
        $reduce = new MongoCode ( '
                function(key,emits){
                    var total=0;
                    for(var i in emits){
                        total+=emits[i].count;
                    }
                    return {"channel":emits[0].channel,"prop_id":eval(emits[0].prop_id),"count":total};
                }
                ' );
$this->mongo_db->command ( array ('mapreduce' => $collection_name, 'map' => $map, 'reduce' => $reduce, 'out' => $tmp_result, 'query' => $query),array('timeout'=>self::MONGO_CURSOR_TIMEOUT) );
copyright © 萬盛學電腦網 all rights reserved