溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在php中使用mongodb擴展時需要注意哪些問題

發(fā)布時間:2020-12-25 16:30:33 來源:億速云 閱讀:136 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了在php中使用mongodb擴展時需要注意哪些問題,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

最近在使用php的mongo 擴展進行數(shù)據(jù)統(tǒng)計計算,其中有一個時間戳字段,由于精確到了毫秒,長度有13位,但由于開始的時候是以字符串的形式存儲:

復(fù)制代碼 代碼如下:


{ "_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 }


后來改成數(shù)字格式:

復(fù)制代碼 代碼如下:


{ "_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 }


為字符串時,使用下面的查詢是正常的

復(fù)制代碼 代碼如下:


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


但是改為數(shù)字后,使用下面的查詢,死活沒有結(jié)果,但是直接在mongo客戶端直接查詢是有結(jié)果的:

復(fù)制代碼 代碼如下:


db.haozu_success.find({'log.stamp':{$gte:1347346800000,$lt:1347350400000}})


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

復(fù)制代碼 代碼如下:


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


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

復(fù)制代碼 代碼如下:


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


另外,在使用mapreduce進行數(shù)據(jù)統(tǒng)計時,為了防止cursor出現(xiàn)超時異常,還需要設(shè)置一下超時時間

復(fù)制代碼 代碼如下:


$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) );

上述內(nèi)容就是在php中使用mongodb擴展時需要注意哪些問題,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI