![]() |
VOOZH | about |
In MongoDB, the $log10 operator is a powerful tool that allows users to perform mathematical computations directly within the database. This operator returns the base 10 logarithm of a specified number and making it invaluable for data analysis and transformation tasks.
In this article, We will learn about the MongoDB $log10 Operator by understanding various examples in detail and so on.
$log10 operator returns the base 10 logarithms of a specified number. Syntax:
{ $log10: <number> }
Here, the number is a valid expression until it resolves to a non-negative number.
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: example
Document: two documents that contain the details of the shapes in the form of field-value pairs.
Output:
👁 ImageIn this example, we are going to find the log base 10 of the value of the side field in the square document.
db.example.aggregate([{$match:{name: "Square"}},
... {$project: {logbas10: {$log10: "$side"}}}])
Output:
👁 ImageIn this example, we are going to find the log base 10 of the value of the measurement.height field in the rectangle document.
db.example.aggregate([{$match:{name: "Rectangle"}},
... {$project: {logbase10: {$log10: "$measurement.height"}}}])
Output:
The $log10 operator in MongoDB simplifies the process of performing logarithmic calculations within the database, enhancing the efficiency and effectiveness of data analysis tasks. By using this operator, users can perform complex mathematical transformations directly in their MongoDB queries, reducing the need for additional data processing steps outside the database.