修改 - 数据类型
//要改4+2个地方//string转为int类型db.your_collection.find({"your_role":{$type:2}}).forEach(function(x){db.your_collection.update({ your_role : { $type: 2 } }, {$set: {"your_role": NumberInt(x.your_role)}});})
| MongoDB Data types | Number |
|---|---|
| Double | 1 |
| String | 2 |
| Object | 3 |
| Array | 4 |
| Binary data | 5 |
| Undefined | 6 |
| Object Id | 7 |
| Boolean | 9 |
| Date | 10 |
| Null | 11 |
| Regular Expression | 12 |
| JavaScript | 13 |
| symbol | 14 |
| JavaScript with scope | 15 |
| Integer | 16 and 18 |
| timestamp | 10 |
| Min key | 255 |
| Max key | 127 |
//string转为double类型db.your_collection.find({'your_role' : { $type : 2 }}).forEach( function (x) {x.your_role = parseInt(x.your_role);db.your_collection.save(x);});//string转为int类型db.your_collection.find().forEach( function (x) {x.your_role= NumberInt(x.your_role);db.your_collection.save(x);});//string转化为date类型db.your_collection.find().forEach( function (x) {x.your_role = new ISODate(x.your_role);db.your_collection.save(x);});//string转decimaldb.your_collection.find().forEach(function(x) {x.your_role = NumberDecimal(Number(x.your_role));db.your_collection.save(x);})//将其转为string类型db.your_collection.find({your_role:{$exists:true}}).forEach(function(x){x.your_role=new String(x.your_role);db.your_collection.save(x);//保存//更改String类型为Date类型db.getCollection('your_collection').find({'your_role': {$type:2}}).forEach(function(doc){db.getCollection('your_collection').update({'_id': doc._id},{$set:{'your_role': new ISODate(doc.your_role)}})})ordb.getCollection('your_collection').find({'your_role': {$type:2}}).forEach(function(doc){doc.your_role = new ISODate(doc.your_role);db.getCollection('your_collection').save(doc);})//更改Date类型为String类型db.getCollection('your_collection').find({"_id" : 416,'your_role':{$type:9}}).forEach(function(x){x.your_role = x.your_role.toISOString();db.getCollection('your_collection').save(x);})//将类型转为strdb.getCollection('your_collection').find({"_id" : 419}).forEach(function(x){x.your_role = String(x.your_role);db.getCollection('your_collection').save(x);})//截取字符串长度db.getCollection('your_collection').find({"_id" : 416,'your_role':{$type:2}}).forEach(function(x){x.your_role = x.your_role.substr(0,10);db.getCollection('your_collection').save(x);})//更改Date类型为String类型,并截取字符串长度db.getCollection('your_collection').find({"_id" : 416,'your_role':{$type:2}}).forEach(function(x){x.your_role = x.your_role.toISOString().substr(0,10);db.getCollection('your_collection').save(x);})//把时间类型转为NumberLong的时间戳类型db.getCollection('your_collection').find({"_id" : 419,'your_role':{$type:9}}).forEach(function(x){x.your_role = NumberLong(x.your_role.getTime()/1000);db.getCollection('your_collection').save(x);})//设置字段为int类型,NumberInt(3),默认是double类型db.getCollection('your_collection').find({"your_role" : null}).forEach(function(item){db.getCollection('your_collection').update({"_id":item._id},{$set:{"your_role":NumberInt(3)}})})//修改double类型为int类型db.getCollection('your_collection').find({'your_role' : { $type : 1 }}).forEach(function(x) {x.your_role = NumberInt(x.your_role);db.getCollection('your_collection').save(x);})//字符串转为浮点数db.getCollection('your_collection').find({'your_role': {$type:2}}).forEach(function(doc){db.getCollection('your_collection').update({'_id': doc._id},{$set:{'your_role': parseFloat(doc.your_role)}})})//字符串转为doubledb.getCollection('your_collection').find({'your_role': {$type:2}}).forEach(function(doc){db.getCollection('your_collection').update({'_id': doc._id},{$set:{'your_role': parseInt(doc.your_role)}})})