码迷,mamicode.com
首页 > 数据库 > 详细

修改MongDB的数据类型

时间:2019-04-11 19:47:13      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:details   mic   date类   closed   png   数据   isp   als   必须   

语法:

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个是简单的增量值

 

常见的转化

技术图片
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);
    }
)
更改String类型为Date类型
技术图片
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); 
    } 
)
更改Date类型为String类型
技术图片
db.getCollection(‘bond_sentiment_bulletin‘).find({"_id" : 419}).forEach( 
    function(x){ 
        x.status = String(x.status); 
        db.getCollection(‘bond_sentiment_bulletin‘).save(x); 
    } 
)
将类型转为str
技术图片
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); 
    } 
)
把时间类型转为NumberLong的时间戳类型
技术图片
db.getCollection(‘bond_sentiment_bulletin‘).find({‘sentiment‘ : { $type : 1 }}).forEach(
    function(x) {  
        x.sentiment = NumberInt(x.sentiment);
        db.getCollection(‘bond_sentiment_bulletin‘).save(x);  
    }
)
修改double类型为int类型
技术图片
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)}})
    }
)
字符串转为浮点数
技术图片
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)}})
    }
)
字符串转为double

 

参考:

https://blog.csdn.net/xc_zhou/article/details/86644144 

修改MongDB的数据类型

标签:details   mic   date类   closed   png   数据   isp   als   必须   

原文地址:https://www.cnblogs.com/ppzhang/p/10691563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!