$toString (aggregation)

On this page本页内容

Definition定义

$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:语法如下所示:

{
   $toString: <expression>
}

The $toString takes any valid expression.$toString接受任何有效的表达式

The $toString is a shorthand for the following $convert expression:$toString是以下$convert表达式的缩写:

{ $convert: { input: <expression>, to: "string" } }

See also参阅

$convert and $dateToString

Behavior行为

The following table lists the input types that can be converted to a string:下表列出了可以转换为字符串的输入类型:

Input TypeBehavior
Boolean Returns the boolean value as a string.以字符串形式返回布尔值。
Double Returns the double value as a string.以字符串形式返回双精度值。
Decimal Returns the decimal value as a string.以字符串形式返回十进制值。
Integer Returns the integer value as a string.以字符串形式返回整数值。
Long Returns the long value as a string.以字符串形式返回长值。
ObjectId Returns the ObjectId value as a hexadecimal string..以十六进制字符串的形式返回ObjectId值。
String No-op. Returns the string value.无操作。返回字符串值。
Date Returns the date as a string.以字符串形式返回日期。

The following table lists some conversion to string examples:下表列出了一些转换为字符串的示例:

Example示例Results结果
{$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”

Example示例

Create a collection orders with the following documents:使用以下文档创建集合orders

db.orders.insert( [
   { _id: 1, item: "apple",  qty: 5, zipcode: 12345 },
   { _id: 2, item: "pie",  qty: 10, zipcode: 11111 },
   { _id: 3, item: "ice cream",  zipcode: "12345" },
   { _id: 4, item: "almonds", qty: 2, zipcode: "12345-0030" },
])

The following aggregation operation on the orders collection converts the zipcode to string before sorting by the string value:在按字符串值排序之前,orders集合上的以下聚合操作会将zipcode转换为字符串:

// Define stage to add convertedZipCode field with the converted zipcode value

zipConversionStage = {
   $addFields: {
      convertedZipCode: { $toString: "$zipcode" }
   }
};

// Define stage to sort documents by the converted zipcode

sortStage = {
   $sort: { "convertedZipCode": 1 }
};

db.orders.aggregate( [
  zipConversionStage,
  sortStage
])

The operation returns the following documents:该操作将返回以下文档:

{ "_id" : 2, "item" : "pie", "qty" : 10, "zipcode" : 11111, "convertedZipCode" : "11111" }
{ "_id" : 1, "item" : "apple", "qty" : 5, "zipcode" : 12345, "convertedZipCode" : "12345" }
{ "_id" : 3, "item" : "ice cream", "zipcode" : "12345", "convertedZipCode" : "12345" }
{ "_id" : 4, "item" : "almonds", "qty" : 2, "zipcode" : "12345-0030", "convertedZipCode" : "12345-0030" }

Note

If the conversion operation encounters an error, the aggregation operation stops and throws an error. 如果转换操作遇到错误,聚合操作将停止并抛出错误。To override this behavior, use $convert instead.要覆盖此行为,请改用$convert