修改MongDB的数据类型
语法:
db.集合.find({"列":{$type:2}}).forEach(function(x){ x.列=parseFloat(x.列);db.order.save(x) })
db.order.find({"saleprice":{$type:2}}).forEach(function(x){x.saleprice=parseFloat(x.saleprice);db.order.save(x)})
( find().里为数据对应的类型,2表示str。也可以不写 )
mongoDB的数据类型
Object ID :文档的id
String: 字符串,最常用,必须是utf-8
Boolean:布尔值,true 或者false
Integer:整数
Double:浮点数
Arrays:数组或者列表,多个值存储到一个键
Object:用于嵌入文档,即一个值为一个文档
Null:存储null值
Timestamp:时间戳
Date:存储当前日期或时间unix时间格式
Object ID:
每个文档都有一个属性,为_id保证文档的唯一性;
可以自己去设置_id插入文档
如果自己没设置,mongoDB为每个文档提供一个独特的_id ,是一个12字节十六进制数
前4个字节为当前时间戳
接下来的3个字节为机器ID
接下来2个字节为mongo的服务进程ID
最后3个是简单的增量值
常见的转化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
db.getCollection('bond_sentiment_bulletin').find({'pubDate': {$type:2}}).forEach( function(doc){ db.getCollection('bond_sentiment_bulletin').update({'_id': doc._id},{$set:{'pubDate': new ISODate(doc.pubDate)}}) } ) or db.getCollection('bond_sentiment_bulletin').find({'pubDate': {$type:2}}).forEach( function(doc){ doc.pubDate = new ISODate(doc.pubDate); db.getCollection('bond_sentiment_bulletin').save(doc); } )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
db.getCollection('bond_sentiment_bulletin').find({"_id" : 416,'pubDate':{$type:9}}).forEach( function(x){ x.pubDate = x.pubDate.toISOString(); db.getCollection('bond_sentiment_bulletin').save(x); } )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
db.getCollection('bond_sentiment_bulletin').find({"_id" : 419}).forEach( function(x){ x.status = String(x.status); db.getCollection('bond_sentiment_bulletin').save(x); } )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
db.getCollection('bond_sentiment_bulletin').find({"_id" : 419,'pubDate':{$type:9}}).forEach( function(x){ x.pubDate = NumberLong(x.pubDate.getTime()/1000); db.getCollection('bond_sentiment_bulletin').save(x); } )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
db.getCollection('bond_sentiment_bulletin').find({'sentiment' : { $type : 1 }}).forEach( function(x) { x.sentiment = NumberInt(x.sentiment); db.getCollection('bond_sentiment_bulletin').save(x); } )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
db.getCollection('bond_sentiment_bulletin').find({'editTime': {$type:2}}).forEach( function(doc){ db.getCollection('bond_sentiment_bulletin').update({'_id': doc._id},{$set:{'editTime': parseFloat(doc.editTime)}}) } )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
db.getCollection('bond_sentiment_bulletin').find({'editTime': {$type:2}}).forEach( function(doc){ db.getCollection('bond_sentiment_bulletin').update({'_id': doc._id},{$set:{'editTime': parseInt(doc.editTime)}}) } )
参考:
https://blog.csdn.net/xc_zhou/article/details/86644144