修改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)})ide

( find().裏爲數據對應的類型,2表示str。也能夠不寫 )spa

 

 

mongoDB的數據類型

Object  ID :文檔的id.net

String: 字符串,最經常使用,必須是utf-83d

Boolean:布爾值,true 或者falseunix

Integer:整數code

Double:浮點數blog

Arrays:數組或者列表,多個值存儲到一個鍵進程

Object:用於嵌入文檔,即一個值爲一個文檔utf-8

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 

相關文章
相關標籤/搜索