$convert (aggregation)
On this page本页内容
Definition定义
$convert
-
Converts a value to a specified type.将值转换为指定的类型。$convert
has the following syntax:具有以下语法:{
$convert:
{
input: <expression>,
to: <type expression>,
onError: <expression>, // Optional.
onNull: <expression> // Optional.
}
}The$convert
takes a document with the following fields:$convert
获取具有以下字段的文档:Field字段Description描述input
The argument can be any valid expression.参数可以是任何有效的表达式。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。to
The argument can be any valid expression that resolves to one of the following numeric or string identifiers:参数可以是解析为以下数字或字符串标识符之一的任何有效表达式:String Identifier字符串标识符Numeric Identifier数字标识符Notes备注"double" 1 For more information on the conversion to double, see Converting to a Double.有关转换为double的详细信息,请参阅转换为double。"string" 2 For more information on the conversion to string, see Converting to a String.有关转换为字符串的详细信息,请参阅转换为字符串。"objectId" 7 For more information on the conversion to objectId, see Converting to an ObjectId.有关转换为objectId的详细信息,请参阅转换为objectId。"bool" 8 For more information on the conversion to boolean, see Converting to a Boolean.有关转换为布尔值的详细信息,请参阅转换为布尔型。"date" 9 For more information on the conversion to date, see Converting to a Date.有关转换为日期的详细信息,请参阅转换为日期。"int" 16 For more information on the conversion to integer, see Converting to an Integer.有关转换为整数的详细信息,请参阅转换为整数。"long" 18 For more information on the conversion to long, see Converting to a Long.有关转换为长的详细信息,请参阅转换为Long。"decimal" 19 For more information on the conversion to decimal, see Converting to a Decimal.有关转换为十进制的详细信息,请参阅转换为十进制。onError
Optional.可选的。The value to return on encountering an error during conversion, including unsupported type conversions.在转换过程中遇到错误时返回的值,包括不支持的类型转换。The arguments can be any valid expression.参数可以是任何有效的表达式。
If unspecified, the operation throws an error upon encountering an error and stops.如果未指定,则操作在遇到错误时抛出错误并停止。onNull
Optional.可选的。The value to return if the如果input
is null or missing.input
为null
或缺少,则返回的值。The arguments can be any valid expression.参数可以是任何有效的表达式。
If unspecified,如果未指定,如果输入为$convert
returns null if theinput
is null or missing.null
或缺少,$convert
将返回null
。In addition to除了$convert
, MongoDB provides the following aggregation operators as shorthand when the default "onError" and "onNull" behavior is acceptable:$convert
之外,当默认的“onError”和“onNull”行为可以接受时,MongoDB还提供了以下聚合运算符作为简写:
Behavior行为
Converting to a Boolean转换为布尔值
The following table lists the input types that can be converted to a boolean:下表列出了可以转换为布尔值的输入类型:
Boolean | |
Double | true 。false 。 |
Decimal | true 。false 。 |
Integer | true 。false 。 |
Long | true 。false 。 |
ObjectId | true 。 |
String | true 。 |
Date | true 。 |
Timestamp | true 。 |
The following table lists some conversion to boolean examples:下表列出了一些转换为布尔值的示例:
{ input: true, to: "bool" } | true |
{ input: false, to: "bool" } | false |
{ input: 1.99999, to: "bool" } | true |
{ input: Decimal128( "5" ), to: "bool" } | true |
{ input: Decimal128( "0" ), to: "bool" } | false |
{ input: 100, to: "bool" } | true |
{ | true |
{ input: "hello", to: "bool" } | true |
{ input: "false", to: "bool" } | true |
{ input: "", to: "bool" } | true |
{ input: null, to: "bool" } | null |
See also: 另请参阅:
Converting to an Integer转换为整数
The following table lists the input types that can be converted to an integer:下表列出了可以转换为整数的输入类型:
Boolean | 0 for false .false ,则返回0 。1 for true .true ,则返回1 。 |
Double | |
Decimal | |
Integer | |
Long | |
String | "-5" , "123456" ) and fall within the minimum and maximum value for an integer.10 为底的整数(例如"-5" 、"123456" ),并且在整数的最小值和最大值之间。"-5.0" , "0x6400" ) or a value that falls outside the minimum and maximum value for an integer. "-5.0" 、"0x6400" ),也不能转换整数的最小值和最大值之外的值。 |
The following table lists some conversion to integer examples:下表列出了一些转换为整数的示例:
{ input: true, to: "int" } | 1 |
{ input: false, to: "int" } | 0 |
{ input: 1.99999, to: "int" } | 1 |
{ input: Decimal128( "5.5000" ), to: "int" } | 5 |
{ | Error |
{ input: Long( "5000" ), to: "int" } | 5000 |
{ input: Long( "922337203600" ), to: "int" } | Error |
{ input: "-2", to: "int" } | -2 |
{ input: "2.5", to: "int" } | Error |
{ input: null, to: "int" } | null |
See also: 另请参阅:
$toInt
operator.
Converting to a Decimal转换为十进制
The following table lists the input types that can be converted to a decimal:下表列出了可以转换为十进制的输入类型:
Boolean | Decimal128( "0" ) for false .false ,则返回Decimal128( "0" ) 。Decimal128( "1" ) for true .true ,则返回Decimal128( "1" ) |
Double | |
Decimal | |
Integer | |
Long | |
String | "-5.5" , "123456" )."-5.5" 、"123456" )。"0x6400" ) "0x6400" ) |
Date |
The following table lists some conversion to decimal examples:下表列出了一些转换为十进制的示例:
{ input: true, to: "decimal" } | Decimal128("1") |
{ input: false, to: "decimal" } | Decimal128("0") |
{ input: 2.5, to: "decimal" } | Decimal128( "2.50000000000000" ) |
{ input: Int32( 5 ), to: "decimal" } | Decimal128("5") |
{ input: Long( 10000 ), to: "decimal" } | Decimal128("10000") |
{ input: "-5.5", to: "decimal" } | Decimal128("-5.5") |
{ | Decimal128("1522039108044") |
See also: 另请参阅:
Converting to a Double转换为Double
The following table lists the input types that can be converted to a double:下表列出了可以转换为Double的输入类型:
Boolean | false .false ,则返回NumberDouble(0) 。true .true ,则返回NumberDouble(1) 。 |
Double | |
Decimal | |
Integer | |
Long | |
String | "-5.5" , "123456" ) and fall within the minimum and maximum value for a double."-5.5" 、"123456" ),并且在双精度的最小值和最大值之间。"0x6400" ) or a value that falls outside the minimum and maximum value for a double. "0x6400" )或超出最小值和最大值的值转换为双精度。 |
Date |
The following table lists some conversion to double examples:下表列出了一些转换为双重示例:
{ input: true, to: "double" } | 1 |
{ input: false, to: "double" } | 0 |
{ input: 2.5, to: "double" } | 2.5 |
{ input: Int32( 5 ), to: "double" } | 5 |
{ input: Long( "10000" ), to: "double" } | 10000 |
{ input: "-5.5", to: "double" } | -5.5 |
{ input: "5e10", to: "double" } | 50000000000 |
{ | 1522039108044 |
See also: 另请参阅:
Converting to a Long转换为长
The following table lists the input types that can be converted to a long:下表列出了可以转换为long的输入类型:
Boolean | 0 for false .false ,则返回0 。1 for true .1 表示为true 。 |
Double | |
Decimal | |
Integer | |
Long | |
String | "-5" , "123456" ) and fall within the minimum and maximum value for a long."-5" 、"123456" ),并且在长度的最小值和最大值之间。"-5.0" , "0x6400" ) or a value that falls outside the minimum and maximum value for a long. "-5.0" 、"0x6400" ),也不能转换长度在最小值和最大值之外的值。 |
Date |
The following table lists some conversion to long examples:下表列出了一些转换为长示例:
{ input: true, to: "long" } | Long("1") |
{ input: false, to: "long" } | Long("0") |
{ input: 2.5, to: "long" } | Long("2") |
{ input: Decimal128( "5.5000" ), to: "long" } | Long("5") |
{ | Error |
{ input: Int32( 8 ), to: "long" } | Long("8") |
{ | Long("1522039108044") |
{ input: "-2", to: "long" } | Long("-2") |
{ input: "2.5", to: "long" } | Error |
{ input: null, to: "long" } | null |
See also: 另请参阅:
Converting to a Date转换为日期
The following table lists the input types that can be converted to a date:下表列出了可以转换为日期的输入类型:
Double | |
Decimal | |
Long | |
String |
|
ObjectId | |
Timestamp |
The following table lists some conversion to date examples:下表列出了一些转换到日期的示例:
{ | ISODate("1973-10-20T21:20:00.000Z") |
{ | ISODate("2009-09-19T14:53:56.000Z") |
{ | ISODate("2004-11-09T11:33:20.000Z") |
{ | ISODate("1935-02-22T12:26:40.000Z") |
{ | ISODate("2018-03-27T04:08:58.000Z") |
{ input: "2018-03-03", to: "date" } | ISODate("2018-03-03T00:00:00.000Z") |
{ | ISODate("2018-03-20T06:00:06.000Z") |
{ input: "Friday", to: "date" } | Error |
{ | ISODate("2021-11-23T17:21:58.000Z") |
See also: 另请参阅:
$toDate
operator操作人员$dateFromString
Converting to an ObjectId转换为ObjectId
The following table lists the input types that can be converted to an ObjectId:下表列出了可以转换为ObjectId的输入类型:
String |
The following table lists some conversion to date examples:下表列出了一些转换到日期的示例:
{ | ObjectId("5ab9cbfa31c2ab715d42129e") |
{ | Error |
See also: 另请参阅:
$toObjectId
operator.
Converting to a String转换为字符串
The following table lists the input types that can be converted to a string:下表列出了可以转换为字符串的输入类型:
Boolean | |
Double | |
Decimal | |
Integer | |
Long | |
ObjectId | |
String | |
Date |
The following table lists some conversion to string examples:下表列出了一些转换为字符串的示例:
{ input: true, to: "string" } | "true" |
{ input: false, to: "string" } | "false" |
{ input: 2.5, to: "string" } | "2.5" |
{ input: Int32( 2 ), to: "string" } | "2" |
{ input: Long( 1000 ), to: "string" } | "1000" |
{ | "5ab9c3da31c2ab715d421285" |
{ | "2018-03-27T16:58:51.538Z" |
See also: 另请参阅:
$toString
operator操作人员$dateToString
Example实例
Create a collection 使用以下文档创建orders
with the following documents:orders
集合:
db.orders.insertMany( [
{ _id: 1, item: "apple", qty: 5, price: 10 },
{ _id: 2, item: "pie", qty: 10, price: Decimal128("20.0") },
{ _id: 3, item: "ice cream", qty: 2, price: "4.99" },
{ _id: 4, item: "almonds" },
{ _id: 5, item: "bananas", qty: 5000000000, price: Decimal128("1.25") }
] )
The following aggregation operation on the orders
collection converts the price
to a decimal:orders
集合上的以下聚合操作将price
转换为小数:
// Define stage to add convertedPrice and convertedQty fields with
// the converted price and qty values.
// If price or qty values are missing, the conversion returns a
// value of decimal value or int value of 0.
// If price or qty values cannot be converted, the conversion returns
// a string
priceQtyConversionStage = {
$addFields: {
convertedPrice: { $convert:
{
input: "$price",
to: "decimal",
onError: "Error",
onNull: Decimal128("0")
} },
convertedQty: { $convert:
{
input: "$qty",
to: "int",
onError:{ $concat:
[
"Could not convert ",
{ $toString:"$qty" },
" to type integer."
]
},
onNull: Int32("0")
} },
}
};
totalPriceCalculationStage = {
$project: { totalPrice: {
$switch: {
branches: [
{ case:
{ $eq: [ { $type: "$convertedPrice" }, "string" ] },
then: "NaN"
},
{ case:
{ $eq: [ { $type: "$convertedQty" }, "string" ] },
then: "NaN"
},
],
default: { $multiply: [ "$convertedPrice", "$convertedQty" ] }
}
} } };
db.orders.aggregate( [
priceQtyConversionStage,
totalPriceCalculationStage
])
The operation returns the following documents:该操作返回以下文档:
{ _id: 1, totalPrice: Decimal128("50") },
{ _id: 2, totalPrice: Decimal128("200.0") },
{ _id: 3, totalPrice: Decimal128("9.98") },
{ _id: 4, totalPrice: Decimal128("0") },
{ _id: 5, totalPrice: 'NaN' }