更改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}}}})
複制
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