Recently, I am using the mongo extension of PHP to perform 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:
The code is as follows | Copy code | ||||
{ "_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-12345B0DBE6 4" , "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’)); |
The code is as follows | Copy code |
{ "_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 } is a string, it is normal to use the following query $query = array ('log.stamp' => array ('$gte' => '1347346800000', '$lt' => '1347350400000')); |
But after changing to numbers, using the following query, there is no result, but direct query on the mongo client does have results:
代码如下 | 复制代码 |
db.haozu_success.find({'log.stamp':{$gte:1347346800000,$lt:1347350400000}}) |
This is also how it is used in the PHP manual:
代码如下 | 复制代码 |
$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 the manual has an introduction to Types, so the correct usage is as follows:
The code is as follows | Copy code | ||||
|
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
代码如下 | 复制代码 |
$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) ); |