Recently, php's mongo extension was used for data statistics and computation. One of the timestamp fields, precise to milliseconds, has a length of 13 BITs. However, it was originally stored as a string:
{ "_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 it was changed to the numeric format:
{ "_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 }
It is normal to use the following query when it is a string
$query = array ('log.stamp' => array ('$gte' => ‘1347346800000’, '$lt' => ‘1347350400000’));
After changing to a number, you can use the following query to check whether there is any result. However, you can directly query the result on the mongo client:
db.haozu_success.find({'log.stamp':{$gte:1347346800000,$lt:1347350400000}})
This is also the usage in the php manual:
$query = array ('log.stamp' => array ('$gte' => 1347346800000, '$lt' => 1347350400000));
It took a long time to find out the cause. At first, I suspected it was caused by a bug in php extension. after some thought. Suddenly it may be caused by a type problem, and Types is introduced in the manual, so the correct usage 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, to prevent cursor from timeout exceptions, you also need to set the timeout time.
$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) );