Mongodb4.0新特性之聚合操作符 $convert
-
灵活的文档模型是 MongoDB 相比传统关系型数据库的一大优势,应用开发者无需为存储的数据预先定义结构(或者模式),这使得开发者能快速的应对开发需求的迭代;在灵活的同时,MongoDB 还提供了 schema validation 功能,使得开发者可以根据需要定义文档数据的模型。
MongoDB 的文档允许用户灵活的写入各种类型的数据字段,这给消费数据带了一定的复杂性,比如一些数据分析的场景,应用通常希望某个字段的数据拥有统一的类型,以方便数据处理。
MongoDB 4.0 引入了新的聚合操作符 $convert, 允许用户在聚合管道里将文档的字段转换成统一的类型输出,使得数据消费端,比如 MongoDB BI 工具、Spark Connectors 以及其他 ETL 工具能更简单的处理 MongoDB 数据。
将值转换为指定的类型。
$ convert基本语法如下:
{ $convert: { input: <expression>, to: <type expression>, onError: <expression>, //可选的。 onNull: <expression> //可选的。 } }
$ convert获取包含以下字段的文档:
input 参数可以是任何有效的表达式。
to 参数可以是任何解析为以下数字或字符串标识符之一的有效表达式:
字符串标识符
数字标识符
“double”
1
“string”
2
“objectId”
7
“bool”
8
“date”
9
“int”
16
“long”
18
“decimal”
19
onError 可选的。转换期间遇到错误时返回的值,包括不受支持的类型转换。参数可以是任何有效的表达式。如果未指定,则操作在遇到错误时抛出错误并停止。
onNull 可选的。如果输入为null或缺少则返回的值。参数可以是任何有效的表达式。如果未指定,则$convert如果输入为null或缺少则返回null。
除了$ convert之外,MongoDB还提供以下聚合运算符作为默认“onError”和“onNull”行为可接受的简写:
- 本帖下載内容已隐藏,请登入以查看隐藏内容!
转换为Boolean
下表列出了可以转换为布尔值的输入类型:
输入类型
返回结果
Boolean
无操作,返回布尔值。
Double
如果不为零,则返回true。如果为零则返回false。
Decimal
如果不为零,则返回true。如果为零则返回false。
Integer
如果不为零,则返回true。如果为零则返回false。
Long
如果不为零,则返回true。如果为零则返回false。
ObjectId
返回true。
String
返回true。
Date
返回true。
下表为一些转换为布尔值的示例:
示例
返回结果
$convert: { input: false, to: "bool" }
false
$convert: { input: true, to: "bool"}
true
$convert: { input: 3.1415926, to: "bool" }
true
$convert: { input: NumberDecimal("0"), to: "bool"}
false
$convert: { input: NumberDecimal("2"), to: "bool"}
true
$convert: { input: 818, to: "bool" }
true
$convert: { input: ISODate("2018-07-10T03:48:56.023Z"), to: "bool" }
true
$convert: { input: "false", to: "bool" }
true
$convert: { input: "server", to: "bool" }
true
$convert: { input: "", to: "bool" }
true
$convert: { input: null, to: "bool" }
null
转换为Integer
下表列出了可以转换为整数的输入类型:
输入类型
返回结果
Boolean
如果为false则返回0。如果为true则返回1。
Double
返回截断值。
截断的double值必须落在整数的最小值和最大值之内。
您不能转换截断值小于最小整数值或大于最大整数值的double值。
Decimal
返回截断值。
截断的小数值必须落在整数的最小值和最大值之内。
您不能转换截断值小于最小整数值或大于最大整数值的小数值。
Integer
无操作。返回整数值。
Long
以整数形式返回long值。
long值必须落在整数的最小值和最大值之内。
您不能转换小于最小整数值或大于最大整数值的Long值。
String
以整数形式返回字符串的数值。
字符串值必须是base10整数(例如“-6”,“666666”)并且落在整数的最小值和最大值之内。
您不能转换float或decimal或non-base10数字的字符串值(例如“-6.0”,“0x6400”)或超出整数最小值和最大值的值。
下表为一些转换为整数的示例:
示例
返回结果
$convert: { input: false, to: "int" }
0
$convert: { input: true, to: "int"}
1
$convert: { input: 3.1415926, to: " int " }
3
$convert: { input: NumberDecimal("6.6000"), to: "int"}
6
$convert: { input: NumberDecimal("9876543210000.000"), to: "int"}
Error
$convert: { input: NumberDecimal("9223372036000.000"), to: "int", onError: " Could not convert." }
Could not convert.
$convert: { input: NumberLong("8000"), to: "int"}
8000
$convert: { input: NumberLong("9876543210000"), to: "int"}
Error
$convert: { input: "-6", to: "int" }
-6
$convert: { input: "6.6", to: "int" }
Error
$convert: { input: null, to: "int" }
null
转换为Decimal
下表列出了可以转换为小数的输入类型:
输入类型
返回结果
Boolean
如果为false则返回NumberDecimal("0")。如果为true则返回NumberDecimal("1")。
Double
以小数形式返回double值。
Decimal
无操作。返回小数值。
Integer
以小数形式返回整数值。
Long
以小数形式返回long值。
String
以小数形式返回字符串的数值。
字符串值必须是base10数值(例如“-6.6”,“666666”)。
您不能转换非base10数字的字符串值(例如“0x6400”)
Date
返回自纪元以来与日期值对应的小数值。
下表为一些转换为小数的示例:
示例
返回结果
$convert: { input: false, to: "decimal" }
NumberDecimal(“0”)
$convert: { input: true, to: "decimal" }
NumberDecimal(“1”)
$convert: { input: 6.6, to: "decimal" }
NumberDecimal(“6.60000000000000”)
$convert: { input: NumberInt(6), to: "decimal"}
NumberDecimal(“6”)
$convert: { input: NumberLong(66666), to: "decimal"}
NumberDecimal(“66666”)
$convert: { input: "-6.6", to: "decimal" }
NumberDecimal(“-6.6”)
转换为Double
下表列出了可以转换为Double的输入类型:
输入类型
返回结果
Boolean
如果为false则返回NumberLong(0) 。如果为true则返回NumberLong(1) 。
Double
无操作。返回Double值。
Decimal
以double形式返回小数值。
小数值必须落在double的最小值和最大值之内。
您不能转换其值小于最小双精度值或大于最大双精度值的小数值。
Integer
以double形式返回整数值。
Long
以double形式返回long值。
String
以double形式返回字符串的数值。
字符串值必须是base10数值(例如“-6.6”,“666666”),并且落在double的最小值和最大值之内。。
您不能转换非base10数字的字符串值(例如“0x6400”) ,或超出double的最小值和最大值的值。
Date
返回自纪元以来与日期值对应的小数值。
下表为一些转换为Double的示例:
示例
返回结果
$convert: { input: false, to: "double" }
0
$convert: { input: true, to: "double" }
1
$convert: { input: 6.6, to: "double" }
6.6
$convert: { input: NumberInt(6), to: "double"}
6
$convert: { input: NumberLong(66666), to: "double"}
66666
$convert: { input: "-6.6", to: "double" }
-6.6
$convert: { input: "6e10", to: "double" }
60000000000
$convert: { input: "6e660", to: "double", onError: "Could not convert." }
Could not convert.
转换为Long
下表列出了可以转换为Long的输入类型:
输入类型
返回结果
Boolean
如果为false则返回0。如果为true则返回1。
Double
返回截断值。
截断的double值必须在long的最小值和最大值范围内。
您无法转换截断值小于最小长值或大于最大长值的双精度值。
Decimal
返回截断值。
截断的小数值必须落在long的最小值和最大值之内。
您无法转换截断值小于最小长值或大于最大长值的小数值。
Integer
以long形式返回整数值。
Long
无操作。返回long值。
String
返回字符串的数值。
字符串值必须是base10 long(例如“-6”,“666666”),并且落在long的最小值和最大值之内。。
您不能转换float或decimal或non-base10数字的字符串值(例如“-6.0”,“0x6400”) ,或超出long的最小值和最大值的值。
Date
返回自纪元以来与日期值对应的小数值。
下表为一些转换为Long的示例:
示例
返回结果
$convert: { input: false, to: "long" }
NumberLong(“0”)
$convert: { input: true, to: "long" }
NumberLong(“1”)
$convert: { input: 6.6666, to: "long" }
NumberLong(“6”)
$convert: { input: NumberDecimal("6.6000"), to: "long" }
NumberLong(“6”)
$convert: { input: NumberDecimal("9876543210123456789.0"), to: "long"}
Error
$convert: { input: NumberDecimal("9876543210123456789.000"), to:"long", onError: "Could not convert." }
Could not convert.
$convert: { input: NumberInt(8), to: "long" }
NumberLong(“8”)
$convert: { input: "-6", to: "long" }
NumberLong(“-6”)
$convert: { input: "6.6", to: "long" }
Error
$convert: { input: null, to: "long" }
null
转换为Date
下表列出了可以转换为Date的输入类型:
输入类型
返回结果
Double
返回与截断的double值表示的数值对应的日期。
正数对应于自1970年1月1日以来的数值
负数对应于1970年1月1日之前的数值。
Decimal
返回与截断的Decimal值表示的数值对应的日期。
正数对应于自1970年1月1日以来的数值。
负数对应于1970年1月1日之前的数值。
Long
返回与long值表示的数值对应的日期。
正数对应于自1970年1月1日以来的数值。
负数对应于1970年1月1日之前的数值。
String
返回与日期字符串对应的日期。
该字符串必须是有效的日期字符串。
ObjectId
返回与ObjectId的时间戳对应的日期。
下表为一些转换为Date的示例:
示例
返回结果
$convert: { input: 120000000000.5, to: "date"}
ISODate(“1973-10-20T21:20:00Z”)
$convert: { input: NumberLong("1100000000000"), to: "date"}
ISODate(“2004-11-09T11:33:20Z”)
$convert: { input: NumberLong("-1100000000000"), to: "date"}
ISODate(“1935-02-22T12:26:40Z”)
$convert: { input: NumberDecimal("1253372036000.50"), to: "date"}
ISODate(“2009-09-19T14:53:56Z”)
$convert: { input: "2018-03-03", to: "date" }
ISODate(“2018-03-03T00:00:00Z”)
$convert: { input: "Friday", to: "date", onError: "Could not convert." }
Could not convert.
转换为Objectid
下表列出了可以转换为Objectid入类型:
输入类型
返回结果
String
返回长度为24的十六进制字符串的ObjectId。
您不能转换不是长度为24的十六进制字符串的字符串值。
下表为一些转换为Objectid的示例:
示例
返回结果
$convert: { input: "5ab9cbfa31c2ab715d42129e", to: "objectid"}
ObjectId(“5ab9cbfa31c2ab715d42129e”)
$convert: { input: "5ab9cbfa31c2ab715d42129", to: "objectid",onError: "Could not convert." }
Could not convert.
转换为String
下表列出了可以转换为String
输入类型
返回结果
Boolean
以字符串形式返回布尔值。
Double
以字符串形式返回double值。
Decimal
以字符串形式返回decimal值。
Integer
以字符串形式返回整数值。
Long
以字符串形式返回long
String
无操作。返回字符串值。
ObjectId
以十六进制字符串形式返回ObjectId值。
Date
以字符串形式返回日期。
下表为一些转换为String的示例:
示例
返回结果
$convert: { input: false, to: "string" }
“false”
$convert: { input: true, to: "string" }
“true”
$convert: { input: 6.6, to: "string"}
"6.6"
$convert: { input: NumberLong(6666), to: "string"}
"6666"
$convert: { input: ObjectId("5ab9cbfa31c2ab715d42129e "), to: "string"}
“5ab9cbfa31c2ab715d42129e”
$convert: { input: ISODate("2018-07-10T03:48:56.023Z"), to: "string"}
"2018-07-10T03:48:56.023Z"
-
@Jerry 審核沒問題,請通知 @pennyhung 將此文檔放到教學區,謝謝!