Recently, using PHP's MONGO extension for data statistics, there is a timestamp field that has 13 digits in length because it is accurate to milliseconds, but is stored as a string at the beginning:
Copy Code code as follows:
{"_id": ObjectId ("504eea97e4b023cf38e34039"), "In_ts": Numberlong ("1347349143699"), "log": {"GUID": "4d1f3079-7507-f 4b0-e7af-5432d5d8229d "," P ":" View_prop_yeppage_zheng "," CID ":" One "," 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 code as follows:
{"_id": ObjectId ("504eea97e4b023cf38e34039"), "In_ts": Numberlong ("1347349143699"), "log": {"GUID": "4d1f3079-7507-f 4b0-e7af-5432d5d8229d "," P ":" View_prop_yeppage_zheng "," CID ":" One "," 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}
As a string, it is normal to use the following query
Copy Code code as follows:
$query = Array (' Log.stamp ' => array (' $gte ' => ' 1347346800000 ', ' $lt ' => ' 1347350400000 '));
But after the number, the use of the following query, dead or alive without results, but directly in the MONGO client direct query is a result:
Copy Code code as follows:
Db.haozu_success.find ({' Log.stamp ': {$gte: 1347346800000, $lt: 1347350400000}})
This is also the same usage in the PHP manual:
Copy Code code as follows:
$query = Array (' Log.stamp ' => array (' $gte ' => 1347346800000, ' $lt ' => 1347350400000));
Spend a good freshman will find the reason, at the beginning of doubt is the PHP extension of the bug caused, after some thinking. The sudden thought of a possible type problem resulted in a types introduction to the Discovery Manual, so the correct usage is as follows:
Copy Code code as follows:
$query = Array (' Log.stamp ' => array (' $gte ' => ' new MongoInt64 ($time _range[' start '), ' $lt ' => new MongoInt64 ($ti me_range[' End ']));
In addition, when using MapReduce for data statistics, in order to prevent cursor timeout exception, you need to set the timeout time
Copy Code code 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":p rop_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));