On this page本页内容
$toString
¶New in version 4.0.版本4.0中的新功能。
Converts a value to a string. If the value cannot be converted to a string, 将值转换为字符串。如果无法将值转换为字符串,$toString
errors. $toString
错误。If the value is null or missing, 如果值为$toString
returns null.null
或缺少,$toString
返回null
。
$toString
has the following syntax:语法如下所示:
The $toString
takes any valid expression.$toString
接受任何有效的表达式。
The $toString
is a shorthand for the following $convert
expression:$toString
是以下$convert
表达式的缩写:
See also参阅
$convert
and 和$dateToString
The following table lists the input types that can be converted to a string:下表列出了可以转换为字符串的输入类型:
Input Type | Behavior |
---|---|
Boolean | |
Double | |
Decimal | |
Integer | |
Long | |
ObjectId | |
String | |
Date |
The following table lists some conversion to string examples:下表列出了一些转换为字符串的示例:
{$toString: true} |
“true” |
{$toString: false} |
“false” |
{$toString: 2.5} |
“2.5” |
{$toString: NumberInt(2)} |
“2” |
{$toString: NumberLong(1000)} |
“1000” |
{$toString: ObjectId("5ab9c3da31c2ab715d421285")} |
“5ab9c3da31c2ab715d421285” |
{$toString: ISODate("2018-03-27T16:58:51.538Z")} |
“2018-03-27T16:58:51.538Z” |
Create a collection 使用以下文档创建集合orders
with the following documents:orders
:
The following aggregation operation on the 在按字符串值排序之前,orders
collection converts the zipcode
to string before sorting by the string value:orders
集合上的以下聚合操作会将zipcode
转换为字符串:
The operation returns the following documents:该操作将返回以下文档: