MongoDB $atan2

In MongoDB, the $atan2 aggregation pipeline operator returns the arctangent (inverse tangent) of one value divided by another.

You provide the two values in an array. Each of the two values provided to $atan2 can be any valid expression that resolves to a number.

The return value is in radians.

The $atan2 operator was introduced in MongoDB 4.2.

Example

Suppose we have a collection called data with the following document:

{ "_id" : 1, "a" : 2, "b" : 3 }

We can use the $atan2 operator to return the arctangent of the a field divided by the b field:

db.data.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        arctangent: { $atan2: [ "$a", "$b" ] }
      }
    }
  ]
)

Result:

{ "arctangent" : 0.5880026035475675 }

Convert to Degrees

As mentioned, $atan2 returns its result in radians. You can use the $radiansToDegrees operator if you want the result in degrees.

Example:

db.data.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        radians: { $atan2: [ "$a", "$b" ] },
        degrees: { $radiansToDegrees: { $atan2: [ "$a", "$b" ] } }
      }
    }
  ]
)

Result:

{ "radians" : 0.5880026035475675, "degrees" : 33.690067525979785 }

In this example, the first field presents the result in radians, and the second field presents it in degrees.

128-Bit Decimal Values

By default, the $atan2 operator returns values as a double, but it can also return values as a 128-bit decimal as long as the expression resolves to a 128-bit decimal value.

This is the case even when only one of the expressions is 128-bit decimal.

Suppose we add the following documents to our collection:

{ "_id" : 2, "a" : NumberDecimal("1.1301023541559787031443874490659"), "b" : NumberDecimal("2.1301023541559787031443874490659") }
{ "_id" : 3, "a" : 2, "b" : NumberDecimal("2.1301023541559787031443874490659") }
{ "_id" : 4, "a" : NumberDecimal("2.1301023541559787031443874490659"), "b" : 2 }

Let’s run the the $atan2 operator against those documents:

db.data.aggregate(
  [
    { $match: { _id: { $in: [ 2, 3, 4 ] } } },
    { $project: { 
        _id: 0,
        arctangent: { $atan2: [ "$a", "$b" ] }
      }
    }
  ]
)

Result:

{ "arctangent" : NumberDecimal("0.4877792766738730791507215461936449") }
{ "arctangent" : NumberDecimal("0.7539075768401526572881006364456838") }
{ "arctangent" : NumberDecimal("0.8168887499547439619432210551940676") }

In all cases, the output is 128-bit decimal.

Null Values

Null values return null when using the $atan2 operator. This is true even if the only one of the expressions is null.

Suppose we add the following documents to our collection:

{ "_id" : 5, "a" : null, "b" : 2 }
{ "_id" : 6, "a" : 2, "b" : null }
{ "_id" : 7, "a" : 2, "null" : null }

Let’s run the the $atan2 operator against those documents:

db.data.aggregate(
  [
    { $match: { _id: { $in: [ 5, 6, 7 ] } } },
    { $project: { 
        _id: 0,
        arctangent: { $atan2: [ "$a", "$b" ] }
      }
    }
  ]
)

Result:

{ "arctangent" : null }
{ "arctangent" : null }
{ "arctangent" : null }

We can see that the result is null in all cases.

NaN Values

If the argument resolves to NaN$atan2 returns NaN.

Example:

db.data.aggregate(
  [
    { $match: { _id: { $in: [ 2, 3, 4 ] } } },
    { $project: { 
        _id: 0,
        arctangent: { $atan2: [ "$a" * 1, "$b" ] }
      }
    }
  ]
)

Result:

{ "arctangent" : NumberDecimal("NaN") }
{ "arctangent" : NumberDecimal("NaN") }
{ "arctangent" : NaN }

Let’s change it slightly, so that we multiply field b instead of field a.

db.data.aggregate(
  [
    { $match: { _id: { $in: [ 2, 3, 4 ] } } },
    { $project: { 
        _id: 0,
        arctangent: { $atan2: [ "$a", "$b" * 1 ] }
      }
    }
  ]
)

Result:

{ "arctangent" : NumberDecimal("NaN") }
{ "arctangent" : NaN }
{ "arctangent" : NumberDecimal("NaN") }

And now let’s multiply both fields:

db.data.aggregate(
  [
    { $match: { _id: { $in: [ 2, 3, 4 ] } } },
    { $project: { 
        _id: 0,
        arctangent: { $atan2: [ "$a" * 1, "$b" * 1 ] }
      }
    }
  ]
)

Result:

{ "arctangent" : NaN }
{ "arctangent" : NaN }
{ "arctangent" : NaN }

Non-Existent Fields

If the $atan2 operator is applied against a field that doesn’t exist, null is returned.

Example:

db.data.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        result: { $atan2: [ "$a", "$name" ] }
      }
    }
  ]
)

Result:

{ "result" : null }