前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB更改字段类型

MongoDB更改字段类型

作者头像
周小董
发布2019-03-25 10:28:50
6.5K0
发布2019-03-25 10:28:50
举报
文章被收录于专栏:python前行者python前行者

更改String类型为Date类型

代码语言:javascript
复制
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类型

代码语言:javascript
复制
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

代码语言:javascript
复制
db.getCollection('bond_sentiment_bulletin').find({"_id" : 419}).forEach( 
    function(x){ 
        x.status = String(x.status); 
        db.getCollection('bond_sentiment_bulletin').save(x); 
    } 
)

截取字符串长度

代码语言:javascript
复制
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类型,并截取字符串长度

代码语言:javascript
复制
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的时间戳类型

代码语言:javascript
复制
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类型

代码语言:javascript
复制
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类型

代码语言:javascript
复制
db.getCollection('bond_sentiment_bulletin').find({'sentiment' : { $type : 1 }}).forEach(
    function(x) {  
        x.sentiment = NumberInt(x.sentiment);
        db.getCollection('bond_sentiment_bulletin').save(x);  
    }
)

字符串转为浮点数

代码语言:javascript
复制
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

代码语言:javascript
复制
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的对应表
字段类型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函数,判断某个字段是某个类型

代码语言:javascript
复制
count=0;
db.getCollection('bond_sentiment_bulletin').find({"_id" : 423}).forEach(function(x){if(x.pubDate instanceof Date){count++}});
print(count);

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

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

查询附件某个字段存在的

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • MongoDB支持许多数据类型的列表下面给出:
相关产品与服务
云数据库 MongoDB
腾讯云数据库 MongoDB(TencentDB for MongoDB)是腾讯云基于全球广受欢迎的 MongoDB 打造的高性能 NoSQL 数据库,100%完全兼容 MongoDB 协议,支持跨文档事务,提供稳定丰富的监控管理,弹性可扩展、自动容灾,适用于文档型数据库场景,您无需自建灾备体系及控制管理系统。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档