Home >Backend Development >PHP Tutorial >Analysis: Things to note when using php mongodb extension_PHP Tutorial

Analysis: Things to note when using php mongodb extension_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:06:061114browse

Recently, I am using PHP's mongo extension for statistical calculations. There is a timestamp field in it. Since it is accurate to milliseconds, the length is 13 digits, but it is stored in the form of a string at the beginning:

Copy code The code is as follows:

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

Later changed to number format:
Copy code The code is as follows:

{ "_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(104817 80) , "brokerId" : "326792", "tradeType" : "2", "userType" : "0", "channel" : "site", "entry" : "1", "COMMID" : "1666" } } , "out_ts" : NumberLong("1347349466083"), "rule" : 0, "status" : "ok", "txid" : 0 }When

is a string, use the following query It is normal
Copy code The code is as follows:

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

But after changing it to a number, using the following query, there is no result, but directly Direct query on the mongo client will produce results:
Copy code The code is as follows:

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

This is also the usage in the PHP manual:
Copy code The code is as follows:

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

It took me a long time to find the reason. At first, I suspected that it was caused by a bug in the php extension. After some thinking. It suddenly occurred to me that it might be caused by a type problem, and I found that there is an introduction to Types in the manual, so the correct usage is as follows:
Copy the code The code is as follows:

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

In addition, when using mapreduce for data statistics, in order to prevent the cursor from getting a timeout exception, you need to set a timeout
Copy code The code is as follows:

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327638.htmlTechArticle最近在使用php的mongo 扩展进行数据统计计算,其中有一个时间戳字段,由于精确到了毫秒,长度有13位,但由于开始的时候是以字符串的形式...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn