MongoDB更改字段类型

版权声明:如需转载本文章,请保留出处! https://blog.csdn.net/xc_zhou/article/details/86644144

更改String类型为Date类型

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);
    }
)

更改Date类型为String类型

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); 
    } 
)

将类型转为str

db.getCollection('bond_sentiment_bulletin').find({"_id" : 419}).forEach( 
    function(x){ 
        x.status = String(x.status); 
        db.getCollection('bond_sentiment_bulletin').save(x); 
    } 
)

截取字符串长度

db.getCollection('bond_sentiment_bulletin').find({"_id" : 416,'pubDate':{$type:2}}).forEach( 
    function(x){ 
        x.pubDate = x.pubDate.substr(0,10); 
        db.getCollection('bond_sentiment_bulletin').save(x); 
    } 
)

更改Date类型为String类型,并截取字符串长度

db.getCollection('bond_sentiment_bulletin').find({"_id" : 416,'pubDate':{$type:2}}).forEach( 
    function(x){ 
        x.pubDate = x.pubDate.toISOString().substr(0,10); 
        db.getCollection('bond_sentiment_bulletin').save(x); 
    } 
)

把时间类型转为NumberLong的时间戳类型

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); 
    } 
)

设置字段为int类型,NumberInt(3),默认是double类型

db.getCollection('bond_sentiment_bulletin').find({"sentiment" : null}).forEach(
   function(item){                
       db.getCollection('bond_sentiment_bulletin').update({"_id":item._id},{$set:{"sentiment":NumberInt(3)}})
   }
)

修改double类型为int类型

db.getCollection('bond_sentiment_bulletin').find({'sentiment' : { $type : 1 }}).forEach(
    function(x) {  
        x.sentiment = NumberInt(x.sentiment);
        db.getCollection('bond_sentiment_bulletin').save(x);  
    }
)

字符串转为浮点数

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)}})
    }
)

字符串转为double

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)}})
    }
)

字段类型type的对应表如下: 
字段类型type的对应表

字段类型编号:

1 Double 浮点型 
2 String UTF-8字符串都可表示为字符串类型的数据 
3 Object 对象,嵌套另外的文档 
4 Array 值的集合或者列表可以表示成数组 
5 Binary data 二进制 
7 Object id 对象id是文档的12字节的唯一 ID 系统默认会自动生成 
8 Boolean 布尔类型有两个值TRUE和FALSE 
9 Date 日期类型存储的是从标准纪元开始的毫秒数。不存储时区 
10 Null 用于表示空值或者不存在的字段 
11 Regular expression 采用js 的正则表达式语法 
13 JavaScript code 可以存放Javasript 代码 
14 Symbol 符号 
15 JavaScript code with scope 
16 32-bit integer 32位整数类型 
17 Timestamp 特殊语义的时间戳数据类型 
18 64-bit integer 64位整数类型

instanceof函数,判断某个字段是某个类型

count=0;
db.getCollection('bond_sentiment_bulletin').find({"_id" : 423}).forEach(function(x){if(x.pubDate instanceof Date){count++}});
print(count);

查询address字段数据类型为字符串

db.getCollection('bond_sentiment_bulletin').find({address:{$type:2}})         //查询address字段数据类型为字符串
db.getCollection('bond_sentiment_bulletin').find({address:{$type:"string"}})  //查询address字段数据类型为字符串

查询附件某个字段存在的

db.getCollection('bond_sentiment_bulletin').find({'_id':{$gte:587863,$lte:800000},"isPrimary" : 0,'attach':{$elemMatch:{'UpdateTime': {$exists :true}}}})

MongoDB支持许多数据类型的列表下面给出:

  • String : 这是最常用的数据类型来存储数据。在MongoDB中的字符串必须是有效的UTF-8。
  • Integer : 这种类型是用来存储一个数值。整数可以是32位或64位,这取决于您的服务器。
  • Boolean : 此类型用于存储一个布尔值 (true/ false) 。
  • Double : 这种类型是用来存储浮点值。
  • Min/ Max keys : 这种类型被用来对BSON元素的最低和最高值比较。
  • Arrays : 使用此类型的数组或列表或多个值存储到一个键。
  • Timestamp : 时间戳。这可以方便记录时的文件已被修改或添加。
  • Object : 此数据类型用于嵌入式的文件。
  • Null : 这种类型是用来存储一个Null值。
  • Symbol : 此数据类型用于字符串相同,但它通常是保留给特定符号类型的语言使用。
  • Date : 此数据类型用于存储当前日期或时间的UNIX时间格式。可以指定自己的日期和时间,日期和年,月,日到创建对象。
  • Object ID : 此数据类型用于存储文档的ID。
  • Binary data : 此数据类型用于存储二进制数据。
  • Code : 此数据类型用于存储到文档中的JavaScript代码。
  • Regular expression : 此数据类型用于存储正则表达式

官网参考:https://docs.mongodb.com/manual/reference/operator/query/type/index.html

参考:
https://blog.csdn.net/laoyang360/article/details/72594344
https://blog.csdn.net/haiyanggeng/article/details/80250117
https://blog.csdn.net/liangxw1/article/details/78982320
https://blog.csdn.net/xiongzaiabc/article/details/81909771

猜你喜欢

转载自blog.csdn.net/xc_zhou/article/details/86644144
今日推荐