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:
Copy codeCode: {"_ id": ObjectId ("504eea97e4b023cf38e34039"), "in_ts": NumberLong ("1347349143699"), "log": {"guid": "success ", "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:Copy codeCode: {"_ id": ObjectId ("504eea97e4b023cf38e34039"), "in_ts": NumberLong ("1347349143699"), "log": {"guid": "success ", "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 stringCopy codeThe Code is as follows: $ query = array ('Log. stamp' => array ('$ gte' => '000000',' $ lt '=> '000000 '));
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:Copy codeThe Code is as follows: db. haozu_success.find ({'Log. stamp': {$ gte: 1347346800000, $ lt: 1347350400000 }})
This is also the usage in the php manual:Copy codeCode: $ 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:Copy codeThe Code is as follows: $ query = array ('Log. stamp '=> array (' $ gte' => new custom int64 ($ time_range ['start']), '$ lt' => new bytes int64 ($ 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.Copy codeThe Code is as follows: $ map = new response code ('
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 response code ('
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-> sort _db-> command (array ('mapreduce' => $ collection_name, 'map' => $ map, 'reduce' => $ reduce, 'out' => $ tmp_result, 'query' => $ query), array ('timeout' => self: pai_cursor_timeout ));