$log10 - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$log10

New from version 4.0.

The $log10 operator in Amazon DocumentDB is used to calculate the base-10 logarithm of a number. It is useful for performing logarithmic calculations on numeric fields within the aggregation pipeline.

Parameters

  • expression: The numeric expression for which the base-10 logarithm is to be calculated.

Example (MongoDB Shell)

The following example demonstrates how to use the $log10 operator to calculate the base-10 logarithm of a numeric field.

Create sample documents

db.numbers.insertMany([ { _id: 1, value: 1 }, { _id: 2, value: 10 }, { _id: 3, value: 100 }, { _id: 4, value: 1000 } ]);

Query example

db.numbers.aggregate([ { $project: { _id: 1, log10Value: { $log10: "$value" } } } ]);

Output

[ { "_id": 1, "log10Value": 0 }, { "_id": 2, "log10Value": 1 }, { "_id": 3, "log10Value": 2 }, { "_id": 4, "log10Value": 3 } ]

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { let client; try { 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, log10Value: { $log10: "$value" } } } ]).toArray(); console.log(result); } catch (error) { console.error("An error occurred:", error); } finally { if (client) { await client.close(); } } } example();
Python
from pymongo import MongoClient def example(): client = None try: 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, 'log10Value': { '$log10': '$value' } } } ])) print(result) except Exception as e: print(f"An error occurred: {e}") finally: if client: client.close() example()