$toLong

The `$toLong` operator converts a specified value into a Long value.

Syntax

{
  $toLong: <expression>
}

Parameters

expressionobjectrequired

The specified value to convert into a long value.

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "name": "First Up Consultants | Beverage Shop",
  "location": { "lat": -89.2384 }
}

Convert a Double to Long

Convert the latitude field from Double to Long value.

This query truncates a double to a long integer.

Query:

db.stores.aggregate([{
  $match: { _id: "b0107631-9370-4acd-aafa-8ac3511e623d" }
}, {
  $project: {
    originalLatitude: "$location.lat",
    latitudeAsLong: { $toLong: "$location.lat" }
  }
}])

Output:

[
  {
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "originalLatitude": 72.8377,
    "latitudeAsLong": "Long('72')"
  }
]

Convert a String to Long

Convert the string "72" into a Long value.

This query converts a string to a long integer.

Query:

db.stores.aggregate([{
  $match: { _id: "b0107631-9370-4acd-aafa-8ac3511e623d" }
}, {
  $project: {
    originalLatitude: "$location.lat",
    latitudeAsLong: {
      $toLong: { $toString: "72" }
    }
  }
}])

Output:

[
  {
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "originalLatitude": 72.8377,
    "latitudeAsLong": "Long('72')"
  }
]

Related