{"id":15041,"date":"2021-02-26T06:59:32","date_gmt":"2021-02-25T20:59:32","guid":{"rendered":"https:\/\/database.guide\/?p=15041"},"modified":"2021-02-26T06:59:32","modified_gmt":"2021-02-25T20:59:32","slug":"mongodb-atan","status":"publish","type":"post","link":"https:\/\/database.guide\/mongodb-atan\/","title":{"rendered":"MongoDB $atan"},"content":{"rendered":"\n<p>In MongoDB, the <code>$atan<\/code> aggregation pipeline operator returns the arctangent (inverse tangent) of a value.<\/p>\n\n\n\n<p>The return value is in radians.<\/p>\n\n\n\n<p><code>$atan<\/code> accepts any valid expression that resolves to a number.<\/p>\n\n\n\n<p>The <code>$atan<\/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>test<\/code> with the following document:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"_id\" : 1, \"data\" : 2 }<\/pre>\n\n\n\n<p>We can use the <code>$atan<\/code> operator to return the arctangent of the <code>data<\/code> field:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.test.aggregate(\n  &#91;\n    { $match: { _id: 1 } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : 1.1071487177940906 }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Convert to Degrees<\/h2>\n\n\n\n<p>As mentioned, <code>$atan<\/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.test.aggregate(\n  &#91;\n    { $match: { _id: 1 } },\n    { $project: { \n        _id: 0,\n        radians: { $atan: \"$data\" },\n        degrees: { $radiansToDegrees: { $atan: \"$data\" } }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"radians\" : 1.1071487177940906, \"degrees\" : 63.43494882292202 }<\/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&nbsp;<code>$atan<\/code>&nbsp;operator returns values as a&nbsp;<code>double<\/code>, but it can also return values as a&nbsp;128-bit decimal&nbsp;as long as the expression resolves to a 128-bit decimal value.<\/p>\n\n\n\n<p>Suppose we add the following document to our collection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"_id\" : 2, \"data\" : NumberDecimal(\"2.1301023541559787031443874490659\") }<\/pre>\n\n\n\n<p>Let&#8217;s run the the <code>$atan<\/code> operator against that document:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.test.aggregate(\n  &#91;\n    { $match: { _id: 2 } },\n    { $project: { \n        _id: 0,\n        arctangent: { $atan: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"arctangent\" : NumberDecimal(\"1.131877001503761613330938729211760\") }<\/pre>\n\n\n\n<p>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>$atan<\/code> operator.<\/p>\n\n\n\n<p>Suppose we add the following document to our collection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"_id\" : 3, \"data\" : null }<\/pre>\n\n\n\n<p>Let&#8217;s run the the <code>$atan<\/code> operator against that document:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.test.aggregate(\n  &#91;\n    { $match: { _id: 3 } },\n    { $project: { \n        _id: 0,\n        result: { $atan: \"$data\" }\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\n\n\n<p>We can see that the result is <code>null<\/code>. <\/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>$atan<\/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.test.aggregate(\n  &#91;\n    { $match: { _id: 3 } },\n    { $project: { \n        _id: 0,\n        result: { $atan: 1 * \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"result\" : NaN }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Fields<\/h2>\n\n\n\n<p>If the <code>$atan<\/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.test.aggregate(\n  &#91;\n    { $match: { _id: 3 } },\n    { $project: { \n        _id: 0,\n        result: { $atan: \"$wrong\" }\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 $atan aggregation pipeline operator returns the arctangent (inverse tangent) of a value. The return value is in radians. $atan accepts any valid expression that resolves to a number. The $atan operator was introduced in MongoDB 4.2.<\/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-15041","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\/15041","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=15041"}],"version-history":[{"count":8,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/15041\/revisions"}],"predecessor-version":[{"id":15049,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/15041\/revisions\/15049"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=15041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=15041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=15041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}