1 min read

怎么修改 mongodb 字段类型

怎么修改 mongodb 字段类型
Photo by Rubaitul Azad / Unsplash

Mongodb 字段类型对映表

Type Name Type Number
Double 1
String 2
Object 3
Array 4
Binary data 5
Object id 7
Boolean 8
Date 9
Null 10
Regular expression 11
JavaScript code 13
Symbol 14
JavaScript code with scope 15
32-bit integer 16
Timestamp 17
64-bit integer 18
Min key 255
Max key 127

把 published_at 字段 str 类型从改为 Date 类型

db.bitcoin.find( { 'published_at' : { $type : 2 } } ).forEach( function (x) {   
  x.published_at = new Date(x.published_at); // convert field to string
  db.bitcoin.save(x);
});

参考链接