Javatpoint Logo
Javatpoint Logo

MongoDB $log10 Operator

What is the $log10 operator in MongoDB?

MongoDB provides a variety of arithmetic expression operators. The $log10 operator is one of those operators. The $log10 operator is used to calculate the log base 10 of a number, and it returns the result as a double. The $log10 operator was added in MongoDB 3.2 version.

The $log10 operator is equivalent to this expression ( $log : [ < number >, 10 ] ).

Syntax of the $log10 operator:

Where, the < number > can be any valid expression that resolves to a non-negative number (i.e. 0, 1, 2, 3... n).

Important point

  1. If the <number> value is null, it returns null.
  2. If the entered value refers to a missing field, it returns null.
  3. If the <number> value is NaN, it returns NaN.
Example Output
{ $log10 : 1 } 0
{ $log10 : 10 } 1
{ $log10 : 100 } 2
{ $log10 : 1000 } 3
{ $log10 : null } null
{ $log10 : 10 * 1 } NaN

Examples:

Suppose we have a collection of the products with the following documents.

Example 1: Using $log10 Operator

In this example, we are going to calculate the log base 10 of the "Value" field in all "toys" documents using the $log10 operator.

Output:

{
         "_id" : 1, 
         "name" : "toys",
         "value" : 100,
         "log10Value" : 2
}
{
         "_id" : 3, 
         "name" : "toys",
         "value" : 28,
         "log10Value" : 1.44715803134
}
{
         "_id" : 5, 
         "name" : "toys",
         "value" : 30,
         "log10Value" : 1.47712125472
}
{
         "_id" : 8, 
         "name" : "toys",
         "value" : 20,
         "log10Value" : 1.30102999566
}

Example 2: Out of Range Values

The log10 operator accepts only a positive number, and if the value of <number> is negative, it returns an error. In this example, we are going to calculate the log base 10 of the "Value" field in the "headphone" documents using the $log10 operator.

Output:

uncaught exception : Error : command failed : {
	"ok": 0,
	"errmsg": "$log10's argument must be a positive number, but is -10",
	"code": 28761,
	"codeName": "Location28761"
}: aggregate failed:
_getErrorWithCode@src/mongo/shell/utils.js : 25 : 13
doassert@src/mongo/shell/assert.js : 18 : 14
_assertCommandWorked@src/mongo/shell/assert.js : 618 : 17
assert.commandWorked@src/mongo/shell/assert.js : 708 : 16
DB.prototype._runAggregate@src/mongo/shell/db.js : 266 : 5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js : 1046 : 12
@(shell) : 1 : 1

Example 3: Null Values

Null values become zero when using the $log10 operator. Let's run the $log10 operator against the "Value" field in pen documents.

Output:

{
         "name" : "pen",
         "value" : null,
         "log10Value" : null
}
{
         "name" : "pen",
         "value" : 10,
         "log10Value" : 1         
}

Example 4: NaN Values

Output:

{
         "name" : "toys",
         "value" : 100,
         "log10Value" : NaN         
}

Example 5: Non-Existent Fields

If the $log10 operator is applied against a field that does not exist, null is returned.

Output:

{
         "name" : "toys",
         "value" : 28,
         "log10Value" : null         
}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA