$toLong (aggregation)

On this page本页内容

Definition定义

$toLong

New in version 4.0.在版本4.0中新增

Converts a value to a long. 将值转换为longIf the value cannot be converted to a long, $toLong errors. 如果无法将值转换为long,则$toLong错误。If the value is null or missing, $toLong returns null.如果值为null或缺失,$toLong将返回null

$toLong has the following syntax:语法如下:

{
   $toLong: <expression>
}

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

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

{ $convert: { input: <expression>, to: "long" } }
Tip提示
See also: 参阅:

Behavior行为

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

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

Input Type输入类型Behavior行为
Boolean

Returns Long(0) for false.如果为false,则返回Long(0)。

Returns Long(1) for true.true返回Long(1)。

Double

Returns truncated value.返回截断值。

The truncated double value must fall within the minimum and maximum value for a long.被截断的双精度值必须在长时间内处于最小值和最大值之间。

You cannot convert a double value whose truncated value is less than the minimum long value or is greater than the maximum long value.不能转换截断值小于最小长值或大于最大长值的双精度值。

Decimal

Returns truncated value.返回截断值。

The truncated decimal value must fall within the minimum and maximum value for a long.截断的十进制值必须在很长一段时间内处于最小值和最大值之间。

You cannot convert a decimal value whose truncated value is less than the minimum long value or is greater than the maximum long value.不能转换截断值小于最小长值或大于最大长值的十进制值。

IntegerReturns the int value as a long.以long形式返回int值。
LongNo-op. Returns the long value.无。返回长值。
String

Returns the numerical value of the string.返回字符串的数值。

The string value must be of a base 10 long (e.g. "-5", "123456").字符串值必须以10为基数(例如"-5""123456")。

You cannot convert a string value of a float or decimal or non-base 10 number (e.g. "-5.0", "0x6400")不能转换浮点、十进制或非十进制数字的字符串值(例如"-5.0""0x6400"

DateConverts the Date into the number of milliseconds since the epoch.将Date转换为自epoch以来的毫秒数。

The following table lists some conversion to long examples:下表列出了一些转换为长示例的示例:

Example示例Results结果
{ $toLong: true }Long("1")
{ $toLong: false }Long("0")
{ $toLong: 1.99999 }Long("1")
{ $toLong: NumberDecimal("5.5000") }Long("5")
{ $toLong: NumberDecimal("9223372036854775808.0") }Error
{ $toLong: NumberInt(8) }Long(8)
{ $toLong: ISODate("2018-03-26T04:38:28.044Z") }Long("1522039108044")
{ $toLong: "-2" }Long("-2")
{ $toLong: "2.5" }Error
{ $toLong: null }null

Example示例

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

db.orders.insertMany( [
   { _id: 1, item: "apple", qty: NumberInt(5) },
   { _id: 2, item: "pie", qty: "100" },
   { _id: 3, item: "ice cream", qty: NumberLong("500") },
   { _id: 4, item: "almonds", qty: "50" },
] )

The following aggregation operation on the orders collection converts the qty to long before sorting by the value:orders集合上的以下聚合操作在按值排序之前将数量转换为long

// Define stage to add convertedQty field with converted qty value
qtyConversionStage = {
   $addFields: {
      convertedQty: { $toLong: "$qty" }
   }
};
// Define stage to sort documents by the converted qty values
sortStage = {
   $sort: { "convertedQty": -1 }
};
db.orders.aggregate( [
   qtyConversionStage,
   sortStage
])

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

{ _id: 3, item: 'ice cream', qty: Long("500"), convertedQty: Long("500") },
{ _id: 2, item: 'pie', qty: '100', convertedQty: Long("100") },
{ _id: 4, item: 'almonds', qty: '50', convertedQty: Long("50") },
{ _id: 1, item: 'apple', qty: 5, convertedQty: Long("5") }
Note注意

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

←  $toInt (aggregation)$toObjectId (aggregation) →