$convert (aggregation)
在本页面
定义
$convert
版本4.0中的新功能。
将值转换为指定的类型。
$convert
具有以下语法:
在$convert
需要具有以下字段的文档:
除$convert
之外,当默认的“ onError”和“ onNull”行为可以接受时,MongoDB还提供以下聚合运算符作为速记:
$toBool
$toDate
$toDecimal
$toDouble
$toInt
$toLong
$toObjectId
$toString
行为
转换为布尔值
下表列出了可以转换为布尔值的输入类型:
下表列出了一些转换为布尔值的示例:
也可以看看
$toBool
转换为整数
下表列出了可以转换为整数的输入类型:
下表列出了一些转换为整数的示例:
也可以看看
$toInt
操作符。
转换为十进制
下表列出了可以转换为十进制的输入类型:
下表列出了一些转换为十进制的示例:
也可以看看
$toDecimal
转换为Double
下表列出了可以转换为双精度型的输入类型:
下表列出了一些转换为Double的示例:
也可以看看
$toDouble
转换为Long
下表列出了可以转换为long的输入类型:
下表列出了一些到长示例的转换:
也可以看看
$toLong
转换为日期
下表列出了可以转换为日期的输入类型:
下表列出了一些转换日期的示例:
也可以看看
$toDate
操作符,$dateFromString
转换成的ObjectId
下表列出了可以转换为ObjectId的输入类型:
下表列出了一些转换日期的示例:
也可以看看
$toObjectId
操作符。
转换为字符串
下表列出了可以转换为字符串的输入类型:
下表列出了一些转换为字符串的示例:
也可以看看
$toString
操作符。$dateToString
例子
orders
使用以下文档创建一个集合:
集合上的以下汇总操作orders
会将转换price
为小数:
该操作返回以下文档:
译者:李冠飞
校对:
最后更新于