zoukankan      html  css  js  c++  java
  • 修改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个是简单的增量值

    常见的转化

    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 

  • 相关阅读:
    P1486 [NOI2004]郁闷的出纳员
    P1966 火柴排队
    P2627 修剪草坪
    P1621 集合
    P1025 数的划分
    中国剩余定理
    P2043 质因子分解
    P1075 质因数分解
    C#之引用类型参数
    C#之方法的定义及调用学习案例
  • 原文地址:https://www.cnblogs.com/ppzhang/p/10691563.html
Copyright © 2011-2022 走看看