Javatpoint Logo
Javatpoint Logo

MongoDB $ln Operator

What is the $ln operator in MongoDB?

MongoDB provides a variety of arithmetic expression operators. The $ln operator is one of those operators. The $ln operator is used to calculate the natural logarithm (ln, i.e. loge) of a number, and it returns the result as a double. The $ln operator is used in the aggregation-pipeline stages.

The natural logarithm (ln) is the logarithm of the base of the mathematical constant "e", where e is an irrational number starting with 2.7182818284590452353602874713527. The "e" is also known as the Euler number. In JavaScript, we use Math.E to represent "e".

Syntax of the $ln 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
{ $ln : 1 } 0
{ $ln : 10 } 2.30258509299
{ $ln : 100 } 4.60517018599
{ $ln: Math.E } where Math.E is a JavaScript representation for e. 1
{ $ln : null } null
{ $ln : NaN } NaN

Examples:

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

Example 1: Using $ln operator

In this example, we are computing the natural logarithm of a "value" field using the $ln operator.

Output:

{ "name" : "toys", "value" : 10, "natural_log" : 2.30258509299 }
{ "name" : "pen", "value" : 5, "natural_log" : 1.60943791243 }
{ "name" : "phone", "value" : 8, "natural_log" : 2.07944154168 }
{ "name" : "charger", "value" : 20, "natural_log" : 2.99573227355 }
{ "name" : "laptop", "value" : 30, "natural_log" : 3.40119738166 }

Suppose we add the following documents to our collection:

{
        {
         "_id" : 6, 
         "name" : "headphone",
         "value" : -10
        }
        {
         "_id" : 7, 
         "name" : "pen",
         "value" : -20	
        }
}

Example 2: Out of Range Values

In this example, we are computing the natural logarithm of a "value" field using the $ln operator.

Output:

uncaught exception : Error : command failed : {
	"ok": 0,
	"errmsg": "$ln's argument must be a positive number, but is -20",
	"code": 28766,
	"codeName": "Location28766"
}: aggregate failed:
[email protected]/mongo/shell/utils.js : 25 : 13
[email protected]/mongo/shell/assert.js : 18 : 14
[email protected]/mongo/shell/assert.js : 639 : 17
[email protected]/mongo/shell/assert.js : 729 : 16
[email protected]/mongo/shell/db.js : 266 : 5
[email protected]/mongo/shell/collection.js : 1058 : 12
@(shell) : 1 : 1

Suppose we add one more document to our collection:

{
        {
         "_id" : 8, 
         "name" : "phone",
         "value" : null
        }
}

Example 3: Null Values

In this example, we are computing the natural logarithm of a "value" field using the $ln operator.

Output:

{ "name" : "phone", "value" : null, "natural_log" : null }

Example 4: Non-Existent Fields

If the $ln operator is applied against a field that does not exist, null is returned. In this example, we are computing the natural logarithm of a "value" field using the $ln operator.

Output:

{ "name" : "toys", "value" : 10, "natural_log" : 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