无为清净楼资源网 Design By www.qnjia.com
最近在使用php的mongo 扩展进行数据统计计算,其中有一个时间戳字段,由于精确到了毫秒,长度有13位,但由于开始的时候是以字符串的形式存储:
复制代码 代码如下:
{ "_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 }
后来改成数字格式:
复制代码 代码如下:
{ "_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'));
但是改为数字后,使用下面的查询,死活没有结果,但是直接在mongo客户端直接查询是有结果的:
复制代码 代码如下:
db.haozu_success.find({'log.stamp':{$gte:1347346800000,$lt:1347350400000}})
php手册上也是这么个用法:
复制代码 代码如下:
$query = array ('log.stamp' => array ('$gte' => 1347346800000, '$lt' => 1347350400000));
花了好大一会找原因,开始时怀疑是php扩展的bug导致,经过一番思考。突然想到可能是类型问题导致,发现手册上有Types 介绍,所以正确的用法如下:
复制代码 代码如下:
$query = array ('log.stamp' => array ('$gte' => new MongoInt64($time_range['start']), '$lt' => new MongoInt64($time_range['end'])));
另外,在使用mapreduce进行数据统计时,为了防止cursor出现超时异常,还需要设置一下超时时间
复制代码 代码如下:
$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) );
复制代码 代码如下:
{ "_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 }
后来改成数字格式:
复制代码 代码如下:
{ "_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'));
但是改为数字后,使用下面的查询,死活没有结果,但是直接在mongo客户端直接查询是有结果的:
复制代码 代码如下:
db.haozu_success.find({'log.stamp':{$gte:1347346800000,$lt:1347350400000}})
php手册上也是这么个用法:
复制代码 代码如下:
$query = array ('log.stamp' => array ('$gte' => 1347346800000, '$lt' => 1347350400000));
花了好大一会找原因,开始时怀疑是php扩展的bug导致,经过一番思考。突然想到可能是类型问题导致,发现手册上有Types 介绍,所以正确的用法如下:
复制代码 代码如下:
$query = array ('log.stamp' => array ('$gte' => new MongoInt64($time_range['start']), '$lt' => new MongoInt64($time_range['end'])));
另外,在使用mapreduce进行数据统计时,为了防止cursor出现超时异常,还需要设置一下超时时间
复制代码 代码如下:
$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) );
标签:
php,mongodb扩展
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 炉石传说月初最强卡组有哪些 2024月初最强上分卡组推荐
- 狼人杀亮相原生鸿蒙之夜 假面科技强势登陆华为生态
- 12小时光线挑战!AI画质专家才是大平层首选
- 2024游戏IP报告:1~9月规模1960亿 68%用户愿为之付费
- 群星.2024-今夜一起为爱鼓掌电视剧原声带【相信音乐】【FLAC分轨】
- BIGFOUR.2013-大家利事【寰亚】【WAV+CUE】
- 李美凤.1992-情深透全情歌集【EMI百代】【WAV+CUE】
- 田震2024-《时光音乐会》[金峰][WAV+CUE]
- 群星《监听天碟3》[LECD]限量版[WAV+CUE]
- 心妤《声如夏花HQ》头版限量编号[WAV+CUE]
- 群星《摇滚五杰》[低速原抓WAV+CUE][1.1G]
- 群星 《2024好听新歌30》十倍音质 U盘音乐 [WAV+分轨]
- 群星《试音草原·女声篇》经典蒙古民歌[WAV+CUE][1G]
- 陈慧娴《永远是你的朋友》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]
- 曼丽·女人三十《如果·爱》限量1:1母盘直刻[低速原抓WAV+CUE]