db.paymentinfo.update({"_id": ObjectId("55d56fdbe4b0c1f89b5356ae")},{$set:{"paymentTime" : "14400511608049527"}},true);
# 使用遍历数组的方式来操作144开头的时间戳
var ds= db.paymentinfo.find({"paymentTime": {$regex: '144', $options:'i'}});
var dschilds=ds.toArray();
for (var i = 0;i <dschilds.length ; i++) {
var child=dschilds[i];
var id=child._id;
var paymentTime=child.paymentTime;
print(paymentTime);
function tran_val(val){
if(parseInt(val)<10){
val="0" +val;
}
return val;
}
var datenew = new Date(parseInt(paymentTime));
var year=datenew.getFullYear();
var month=tran_val(datenew.getMonth()+1);
var date=tran_val(datenew.getDate());
var hour=tran_val(datenew.getHours());
var minute=tran_val(datenew.getMinutes());
var second=tran_val(datenew.getSeconds());
var datestr=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
# 这里开始进行修改操作
db.paymentinfo.update({"_id": id},{$set:{"paymentTime" :datestr}},true);
db.paymentinfo.find({"_id": id});
}
# 使用遍历数组的方式来操作145开头的时间戳
var ds= db.paymentinfo.find({"paymentTime": {$regex: '145', $options:'i'}});
var dschilds=ds.toArray();
for (var i = 0;i <dschilds.length ; i++) {
var child=dschilds[i];
var id=child._id;
var paymentTime=child.paymentTime;
print(paymentTime);
function tran_val(val){
if(parseInt(val)<10){
val="0" +val;
}
return val;
}
var datenew = new Date(parseInt(paymentTime));
var year=datenew.getFullYear();
var month=tran_val(datenew.getMonth()+1);
var date=tran_val(datenew.getDate());
var hour=tran_val(datenew.getHours());
var minute=tran_val(datenew.getMinutes());
var second=tran_val(datenew.getSeconds());
var datestr=year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
db.paymentinfo.update({"_id": id},{$set:{"paymentTime" :datestr}},true);
db.paymentinfo.find({"_id": id});
}
6,碰到新的问题,统一日期格式,将斜杠变成横杠
–批量修改日期 斜杠变成横杠
var ds= db.paymentinfo.find({"paymentTime": {$regex: '/', $options:'i'}});
var dschilds=ds.toArray();
for (var i = 0;i <dschilds.length; i++) {
var child=dschilds[i];
var id=child._id;
var paymentTime=child.paymentTime;
var paymentTime2=paymentTime.replace(/\//g,"-");
db.paymentinfo.update({"_id": id},{$set:{"paymentTime" :paymentTime2}},true);
print(paymentTime);print(paymentTime2);
db.paymentinfo.find({"_id": id});
}
OK,到此圆满解决,特别感谢给我启发的网友 Aeolus@普 ,^_^
更多MongoDB相关教程见以下内容:
CentOS 编译安装 MongoDB与mongoDB的php扩展
CentOS 6 使用 yum 安装MongoDB及服务器端配置
Ubuntu 13.04下安装MongoDB2.4.3