On this page本页内容
$convert
¶New in version 4.0.版本4.0中的新功能。
Converts a value to a specified type.将值转换为指定的类型。
$convert
has the following syntax:语法如下所示:
The $convert
takes a document with the following fields:$convert
接受包含以下字段的文档:
input |
||||||||||||||||||||||||||||
to |
| |||||||||||||||||||||||||||
onError |
| |||||||||||||||||||||||||||
onNull |
|
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还提供以下聚合运算符作为简写:
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 。 |
The following table lists some conversion to boolean examples:下表列出了一些转换为布尔值的示例:
true | |
false | |
true | |
true | |
false | |
true | |
true | |
true | |
true | |
true | |
null |
See also参阅
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 |
|
The following table lists some conversion to integer examples:下表列出了一些转换为整数的示例:
1 | |
0 | |
1 | |
5 | |
Error | |
5000 | |
Error | |
-2 | |
Error | |
null |
See also参阅
$toInt
operator.操作人员
The following table lists the input types that can be converted to a decimal:下表列出了可转换为十进制的输入类型:
Boolean | NumberDecimal("0") for false .false 返回NumberDecimal("0") 。NumberDecimal("1") for true .true 返回NumberDecimal("1") 。 |
Double | |
Decimal | |
Integer | |
Long | |
String |
|
Date |
The following table lists some conversion to decimal examples:下表列出了一些十进制转换示例:
NumberDecimal(“1”) | |
NumberDecimal(“0”) | |
NumberDecimal(“2.50000000000000”) | |
NumberDecimal(“5”) | |
NumberDecimal(“10000”) | |
NumberDecimal(“-5.5”) | |
NumberDecimal(“1522127087890”) |
See also参阅
The following table lists the input types that can be converted to a double:下表列出了可以转换为双精度输入的输入类型:
Boolean | false .false 返回NumberLong(0) 。true .true 返回NumberLong(1) 。 |
Double | |
Decimal |
|
Integer | |
Long | |
String |
|
Date |
The following table lists some conversion to double examples:下表列出了一些转换为双重示例:
1 | |
0 | |
2.5 | |
5 | |
10000 | |
-5.5 | |
50000000000 | |
“Could not convert to type double.” | |
1522127087890 |
See also参阅
The following table lists the input types that can be converted to a long:下表列出了可转换为long的输入类型:
Boolean | 0 for false .false 返回0 。1 for true .true 返回1 。 |
Double |
|
Decimal |
|
Integer | |
Long | |
String |
|
Date |
The following table lists some conversion to long examples:下表列出了一些到长示例的转换:
NumberLong(“1”) | |
NumberLong(“0”) | |
NumberLong(“1”) | |
NumberLong(“5”) | |
Error | |
“Could not convert to type long.” | |
NumberLong(8) | |
NumberLong(“1522039108044”) | |
NumberLong(“-2”) | |
Error | |
null |
See also参阅
The following table lists the input types that can be converted to a date:下表列出了可转换为日期的输入类型:
Double |
|
Decimal |
|
Long |
|
String |
|
ObjectId |
The following table lists some conversion to date examples:下表列出了一些到日期的转换示例:
ISODate(“1973-10-20T21:20:00Z”) | |
ISODate(“2009-09-19T14:53:56Z”) | |
ISODate(“2004-11-09T11:33:20Z”) | |
ISODate(“1935-02-22T12:26:40Z”) | |
ISODate(“2018-03-27T04:08:58Z”) | |
ISODate(“2018-03-03T00:00:00Z”) | |
ISODate(“2018-03-20T06:00:06Z”) | |
Error | |
See also参阅
$toDate
operator, $dateFromString
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.
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:下表列出了一些转换为字符串的示例:
“true” | |
“false” | |
“2.5” | |
“2” | |
“1000” | |
“5ab9c3da31c2ab715d421285” | |
“2018-03-27T16:58:51.538Z” |
See also参阅
$toString
operator. $dateToString
Create a collection 使用以下文档创建集合orders
with the following documents:orders
:
The following aggregation operation on the orders
collection converts the price
to a decimal:orders
集合上的以下聚合操作将price
转换为十进制:
The operation returns the following documents:该操作将返回以下文档: