mongoDB修改字段类型:string转decimal

将test表中的num_string字段从String转换到decimal:

db.test.find().forEach(function(x){x.num_string=NumberDecimal(Number(x.num_string));db.test.save(x);})

为了严谨,只转换字段是特定类型的数据

find指定数据类型的字段:

db.test.find({‘num_string’ : { $type : 16 }})

将指定数据类型的字段转换:

db.test.find({‘num_string’ : { $type : 2 }}).forEach(function(x){x.num_string=NumberDecimal(Number(x.num_string));db.test.save(x);})

mongodb3.4字段类型编号:

Type Number Alias Notes
Double 1 “double”
String 2 “string”
Object 3 “object”
Array 4 “array”
Binary data 5 “binData”
Undefined 6 “undefined” Deprecated.
ObjectId 7 “objectId”
Boolean 8 “bool”
Date 9 “date”
Null 10 “null”
Regular Expression 11 “regex”
DBPointer 12 “dbPointer” Deprecated.
JavaScript 13 “javascript”
Symbol 14 “symbol” Deprecated.
JavaScript (with scope) 15 “javascriptWithScope”
32-bit integer 16 “int”
Timestamp 17 “timestamp”
64-bit integer 18 “long”
Decimal128 19 “decimal” New in version 3.4.
Min key -1 “minKey”
Max key 127 “maxKey”

2 条评论

[/0o0] [..^v^..] [0_0] [T.T] [=3-❤] [❤.❤] [^v^] [-.0] [!- -] [=x=] [→_→] [><] 更多 »
昵称
  1. Pingback: 谨慎 mongodb 关于数字操作可能导致类型及精度变化 – FIXBBS

  2. eyenewer Google Chrome 83 Google Chrome 83 Windows 7 Windows 7

    怎么转回去呢???? [..^v^..]