Javatpoint Logo
Javatpoint Logo

MongoDB $log Operator

What is the $log operator?

MongoDB provides a variety of arithmetic expression operators. The $log operator is one of those operators. The $log operator is used in the aggregation-pipeline stages. This operator is also used to calculate the log of a number in a given base and return the output as a double.

Syntax of the $log operator:

The <number> can be any valid expression that resolves to a non-negative number (0, 1, 2, 3....), and <base> can be any valid expression that resolves for a positive number greater than 1.

Important point:

  1. If the argument value is null, the $abs operator returns null.
  2. If the entered value refers to a missing field, the $abs operator returns null.
  3. If the argument value is NaN, the $abs operator returns NaN.
Example Output
{ $log : [ 100, 10 ] } 2
{$log : [ 200, 10 ] } 2.301029995663981
{$log : [ 10, 2 ] } 3.3219280948874
{ $log: [ 100, Math.E ] } (Where, Math.E is a JavaScript representation for e) 4.605170185988092

Examples:

In the following examples, we are working with:

>db.example1.find().pretty()
{
        {
         "_id" : ObjectId("56254d4fdf2222265r4g1hb78452"), 
         "name" : "rectangle",
         "area" : 16
        }
        {
         "_id" : ObjectId("56254d4fdf2222265r4g1hb71478"), 
         "name" : "rectangle",
         "area" : 6
        }
        {
         "_id" : ObjectId("56254d4fdf2222265r4g1789654"), 
         "name" : "circle",
         "area" : 19,
         "unit" : { 
                           "diameter" : 6,
                           "radius" : 3
                         }
        }
        {
         "_id" : ObjectId("56254d4fdf2222265r4g1987412"), 
         "name" : "rectangle",
         "area" : 20
        }
        {
         "_id" : ObjectId("56254d4fdf2222265r4g1987412"), 
         "name" : "square",
         "area" : 20
        }
        {
         "_id" : ObjectId("56254d4fdf2222265r4g1987f15"), 
         "name" : "triangle",
         "area" : null
        }
}

Example 1: Using $log operator

In this example, we're going to find the log value of the area field in each rectangle, and the base value of the log is 10.

Output:

{
     "_id" : ObjectId("56254d4fdf2222265r4g1hb78452"), 
     "name" : "rectangle",
     "area" : 16,
     "logArea" : 1.204119982655925
}
{
     "_id" : ObjectId("56254d4fdf2222265r4g1789654"), 
     "name" : " rectangle",
     "area" : 6,
     "logArea" : 0.778151250383644
}
{
     "_id" : ObjectId("56254d4fdf2222265r4g1987412"), 
     "name" : "rectangle",
     "area" : 20,
     "logArea" : 1.301029995663981
} 

Example 2: Using $log operator (embedded document)

In this example, we're going to find the log value of the unit.radius field in the circle, and the base value of the log is 2.

Output:

{
         "_id" : ObjectId("56254d4fdf2222265r4g1789654"), 
         "name" : "cricle",
         "area" : 19,
         "unit" : { "radius" : 3 },
         "logRadius" : 1.5849625007212
}  

Example 3: Missing fields

In this example, we're going to find the log value of the perimeter field in the square, and the base value of the log is 10.

Output:

{
         "_id" : ObjectId("56254d4fdf2222265r4g1987412"), 
         "name" : "square",
         "area" : 20,
         "perimeter" : null  
}    

Example 4: Null value

In this example, we're going to find the log value of the area field in the triangle, and the base value of the log is 16.

Output:

{
         "_id" : ObjectId("56254d4fdf2222265r4g1987f15"), 
         "name" : "triangle",
         "area" : null,
         "logArea" : 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