{"id":15050,"date":"2021-02-26T06:59:40","date_gmt":"2021-02-25T20:59:40","guid":{"rendered":"https:\/\/database.guide\/?p=15050"},"modified":"2021-02-26T06:59:40","modified_gmt":"2021-02-25T20:59:40","slug":"mongodb-atan2","status":"publish","type":"post","link":"https:\/\/database.guide\/mongodb-atan2\/","title":{"rendered":"MongoDB $atan2"},"content":{"rendered":"\n<p>In MongoDB, the <code>$atan2<\/code> aggregation pipeline operator returns the arctangent (inverse tangent) of one value divided by another.<\/p>\n\n\n\n<p>You provide the two values in an array. Each of the two values provided to <code>$atan2<\/code> can be any valid expression that resolves to a number. <\/p>\n\n\n\n<p>The return value is in radians.<\/p>\n\n\n\n<p>The <code>$atan2<\/code> operator was introduced in MongoDB 4.2.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Suppose we have a collection called <code>data<\/code> with the following document:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"_id\" : 1, \"a\" : 2, \"b\" : 3 }<\/pre>\n\n\n\n<p>We can use the <code>$atan2<\/code> operator to return the arctangent of the <code>a<\/code> field divided by the <code>b<\/code> field:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: 1 } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan2: &#91; \"$a\", \"$b\" ] }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : 0.5880026035475675 }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Convert to Degrees<\/h2>\n\n\n\n<p>As mentioned, <code>$atan2<\/code> returns its result in radians. You can use the <code><a href=\"https:\/\/database.guide\/mongodb-radianstodegrees\/\" title=\"MongoDB $radiansToDegrees\">$radiansToDegrees<\/a><\/code> operator if you want the result in degrees.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: 1 } },\n    { $project: { \n        _id: 0,\n        radians: { $atan2: &#91; \"$a\", \"$b\" ] },\n        degrees: { $radiansToDegrees: { $atan2: &#91; \"$a\", \"$b\" ] } }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"radians\" : 0.5880026035475675, \"degrees\" : 33.690067525979785 }<\/pre>\n\n\n\n<p>In this example, the first field presents the result in radians, and the second field presents it in degrees.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">128-Bit Decimal Values<\/h2>\n\n\n\n<p>By default, the\u00a0<code>$atan2<\/code>\u00a0operator returns values as a\u00a0<code>double<\/code>, but it can also return values as a\u00a0128-bit decimal\u00a0as long as the expression resolves to a 128-bit decimal value.<\/p>\n\n\n\n<p>This is the case even when only one of the expressions is 128-bit decimal.<\/p>\n\n\n\n<p>Suppose we add the following documents to our collection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{ \"_id\" : 2, \"a\" : NumberDecimal(\"1.1301023541559787031443874490659\"), \"b\" : NumberDecimal(\"2.1301023541559787031443874490659\") }\n{ \"_id\" : 3, \"a\" : 2, \"b\" : NumberDecimal(\"2.1301023541559787031443874490659\") }\n{ \"_id\" : 4, \"a\" : NumberDecimal(\"2.1301023541559787031443874490659\"), \"b\" : 2 }<\/code><\/pre>\n\n\n\n<p>Let&#8217;s run the the <code>$atan2<\/code> operator against those documents:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: { $in: &#91; 2, 3, 4 ] } } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan2: &#91; \"$a\", \"$b\" ] }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : NumberDecimal(\"0.4877792766738730791507215461936449\") }\n{ \"arctangent\" : NumberDecimal(\"0.7539075768401526572881006364456838\") }\n{ \"arctangent\" : NumberDecimal(\"0.8168887499547439619432210551940676\") }<\/pre>\n\n\n\n<p>In all cases, the output is 128-bit decimal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Null Values<\/h2>\n\n\n\n<p>Null values return <code>null<\/code> when using the <code>$atan2<\/code> operator. This is true even if the only one of the expressions is <code>null<\/code>.<\/p>\n\n\n\n<p>Suppose we add the following documents to our collection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"_id\" : 5, \"a\" : null, \"b\" : 2 }\n{ \"_id\" : 6, \"a\" : 2, \"b\" : null }\n{ \"_id\" : 7, \"a\" : 2, \"null\" : null }<\/pre>\n\n\n\n<p>Let&#8217;s run the the <code>$atan2<\/code> operator against those documents:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: { $in: &#91; 5, 6, 7 ] } } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan2: &#91; \"$a\", \"$b\" ] }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : null }\n{ \"arctangent\" : null }\n{ \"arctangent\" : null }<\/pre>\n\n\n\n<p>We can see that the result is <code>null<\/code> in all cases. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">NaN Values<\/h2>\n\n\n\n<p>If the argument resolves to\u00a0<code>NaN<\/code>,\u00a0<code>$atan2<\/code>\u00a0returns\u00a0<code>NaN<\/code>.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: { $in: &#91; 2, 3, 4 ] } } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan2: &#91; \"$a\" * 1, \"$b\" ] }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : NumberDecimal(\"NaN\") }\n{ \"arctangent\" : NumberDecimal(\"NaN\") }\n{ \"arctangent\" : NaN }<\/pre>\n\n\n\n<p>Let&#8217;s change it slightly, so that we multiply field <code>b<\/code> instead of field <code>a<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: { $in: &#91; 2, 3, 4 ] } } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan2: &#91; \"$a\", \"$b\" * 1 ] }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : NumberDecimal(\"NaN\") }\n{ \"arctangent\" : NaN }\n{ \"arctangent\" : NumberDecimal(\"NaN\") }<\/pre>\n\n\n\n<p>And now let&#8217;s multiply both fields:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: { $in: &#91; 2, 3, 4 ] } } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan2: &#91; \"$a\" * 1, \"$b\" * 1 ] }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : NaN }\n{ \"arctangent\" : NaN }\n{ \"arctangent\" : NaN }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Fields<\/h2>\n\n\n\n<p>If the <code>$atan2<\/code> operator is applied against a field that doesn&#8217;t exist, <code>null<\/code> is returned.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.data.aggregate(\n  &#91;\n    { $match: { _id: 1 } },\n    { $project: { \n        _id: 0,\n        result: { $atan2: &#91; \"$a\", \"$name\" ] }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"result\" : null }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <a title=\"MongoDB $atan2\" class=\"read-more\" href=\"https:\/\/database.guide\/mongodb-atan2\/\" aria-label=\"Read more about MongoDB $atan2\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[113,85,20],"class_list":["post-15050","post","type-post","status-publish","format-standard","hentry","category-mongodb","tag-aggregation","tag-operators","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/15050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=15050"}],"version-history":[{"count":4,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/15050\/revisions"}],"predecessor-version":[{"id":15054,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/15050\/revisions\/15054"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=15050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=15050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=15050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}