$toDouble - Amazon DocumentDB

$toDouble

New from version 4.0

The $toDouble operator in Amazon DocumentDB is used to convert a value to a double-precision 64-bit floating-point number. This can be useful when you need to perform arithmetic operations on values that are not originally in a numeric format.

Parameters

<expression>: The expression to convert to a double value. This can be any valid expression that resolves to a numeric, string, or boolean value.

Example (MongoDB Shell)

This example demonstrates how to use the $toDouble operator to convert a string value to a numeric value for the purpose of performing a mathematical calculation.

Create sample documents

db.numbers.insertMany([ { _id: 1, value: "10.5" }, { _id: 2, value: "20.25" }, { _id: 3, value: "7" } ])

Query example

db.numbers.aggregate([ { $project: { _id: 1, value: 1, double_value: { $toDouble: "$value" }, double_plus_five: { $add: [{ $toDouble: "$value" }, 5] } } } ])

Output

[ { "_id" : 1, "value" : "10.5", "double_value" : 10.5, "double_plus_five" : 15.5 }, { "_id" : 2, "value" : "20.25", "double_value" : 20.25, "double_plus_five" : 25.25 }, { "_id" : 3, "value" : "7", "double_value" : 7.0, "double_plus_five" : 12.0 } ]

Code examples

To view a code example for using the $toDouble command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('numbers'); const result = await collection.aggregate([ { $project: { _id: 1, value: 1, double_value: { $toDouble: "$value" }, double_plus_five: { $add: [{ $toDouble: "$value" }, 5] } } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['numbers'] result = list(collection.aggregate([ { '$project': { '_id': 1, 'value': 1, 'double_value': { '$toDouble': '$value' }, 'double_plus_five': { '$add': [{ '$toDouble': '$value' }, 5] } } } ])) print(result) client.close() example()