$toLong (aggregation)

Definition

  • $toLong

New in version 4.0.

Converts a value to a long. If the value cannot be convertedto a long, $toLong errors. If the value is null ormissing, $toLong returns null.

$toLong has the following syntax:

  1. {
  2. $toLong: <expression>
  3. }

The $toLong takes any valid expression.

The $toLong is a shorthand for the following$convert expression:

  1. { $convert: { input: <expression>, to: "long" } }

See also

$convert

Behavior

The following table lists the input types that can be converted to adecimal:

The following table lists the input types that can be converted to along:

Input TypeBehavior
BooleanReturns NumberLong(0) for false.Returns NumberLong(1) for true.
DoubleReturns truncated value.The truncated double value must fall within the minimum andmaximum value for a long.You cannot convert a double value whose truncated value is lessthan the minimum long value or is greater than the maximumlong value.
DecimalReturns truncated value.The truncated decimal value must fall within the minimum andmaximum value for a long.You cannot convert a decimal value whose truncated value is lessthan the minimum long value or is greater than the maximumlong value.
IntegerReturns the int value as a long.
LongNo-op. Returns the long value.
StringReturns the numerical value of the string.The string value must be of a base10 long (e.g."-5", "123456").You cannot convert a string value of a float or decimal ornon-base10 number (e.g. "-5.0", "0x6400")
DateConverts the Date into the number of milliseconds since theepoch.

The following table lists some conversion to long examples:

ExampleResults
{ $toLong: true }NumberLong(“1”)
{ $toLong: false }NumberLong(“0”)
{ $toLong: 1.99999 }NumberLong(“1”)
{ $toLong: NumberDecimal("5.5000") }NumberLong(“5”)
{ $toLong: NumberDecimal("9223372036854775808.0") }Error
{ $toLong: NumberInt(8) }NumberLong(8)
{ $toLong: ISODate("2018-03-26T04:38:28.044Z") }NumberLong(“1522039108044”)
{ $toLong: "-2" }NumberLong(“-2”)
{ $toLong: "2.5" }Error
{ $toLong: null }null

Example

Create a collection orders with the following documents:

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

The following aggregation operation on the orders collectionconverts the qty to long before sorting by the value:

  1. // Define stage to add convertedQty field with converted qty value
  2.  
  3. qtyConversionStage = {
  4. $addFields: {
  5. convertedQty: { $toLong: "$qty" }
  6. }
  7. };
  8.  
  9. // Define stage to sort documents by the converted qty values
  10.  
  11. sortStage = {
  12. $sort: { "convertedQty": -1 }
  13. };
  14.  
  15.  
  16. db.orders.aggregate( [
  17. qtyConversionStage,
  18. sortStage
  19. ])

The operation returns the following documents:

  1. { "_id" : 1, "item" : "apple", "qty" : 5, "convertedQty" : NumberLong(5) }
  2. { "_id" : 2, "item" : "pie", "qty" : "100", "convertedQty" : NumberLong(100) }
  3. { "_id" : 3, "item" : "ice cream", "qty" : NumberLong(500), "convertedQty" : NumberLong(500) }
  4. { "_id" : 4, "item" : "almonds", "qty" : "50", "convertedQty" : NumberLong(50) }

Note

If the conversion operation encounters an error, the aggregationoperation stops and throws an error. To override this behavior, use$convert instead.