$toDouble

The `$toDouble` operator converts a specified value into a Double value.

Syntax

{
  $toDouble: <expression>
}

Parameters

expressionobjectrequired

The specified value to convert into a Double value.

Examples

Sample Data

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

Convert a String to Double

Convert the string representation of "72" to a double value.

This query converts a string to a double numeric value.

Query:

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

Output:

[
  {
    "_id": "b0107631-9370-4acd-aafa-8ac3511e623d",
    "originalLatitude": 72.8377,
    "latitudeAsDouble": 72
  }
]

Related