更改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的对应表如下:
字段类型编号:
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}}}})
官网参考: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