{"id":14975,"date":"2021-02-25T07:58:16","date_gmt":"2021-02-24T21:58:16","guid":{"rendered":"https:\/\/database.guide\/?p=14975"},"modified":"2021-02-25T07:58:17","modified_gmt":"2021-02-24T21:58:17","slug":"mongodb-acosh","status":"publish","type":"post","link":"https:\/\/database.guide\/mongodb-acosh\/","title":{"rendered":"MongoDB $acosh"},"content":{"rendered":"\n<p>In MongoDB, the <code>$acosh<\/code> aggregation pipeline operator returns the hyperbolic&nbsp;arccosine (inverse hyperbolic&nbsp;cosine) of a value, measured in radians.<\/p>\n\n\n\n<p><code>$acosh<\/code> accepts any valid expression that resolves to a number between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>+Infinity<\/code>.<\/p>\n\n\n\n<p>The <code>$acosh<\/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\" : 3 }<\/pre>\n\n\n\n<p>We can use the <code>$acosh<\/code> operator to return the hyperbolic arccosine of the <code>data<\/code> field:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.test.aggregate(\n  &#91;\n    { $project: { \n        _id: 0,\n        hyperbolicArccosine: { $acosh: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicArccosine\" : 1.762747174039086 }<\/pre>\n\n\n\n<p>By default, the&nbsp;<code>$acosh<\/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<h2 class=\"wp-block-heading\">Convert to Degrees<\/h2>\n\n\n\n<p>As mentioned, <code>$acosh<\/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    { $project: { \n        _id: 0,\n        radians: { $acosh: \"$data\" },\n        degrees: { $radiansToDegrees: { $acosh: \"$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.762747174039086, \"degrees\" : 100.99797342105244 }<\/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\">Out of Range Values<\/h2>\n\n\n\n<p>Providing an out of range value to <code>$acosh<\/code> will result in an error.<\/p>\n\n\n\n<p>Suppose we add the following document to the collection:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"_id\" : 2, \"data\" : 0 }<\/pre>\n\n\n\n<p>Now let&#8217;s run <code>$acosh<\/code> against the <code>data<\/code> field:<\/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        radians: { $acosh: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">uncaught exception: Error: command failed: {\n\t\"ok\" : 0,\n\t\"errmsg\" : \"cannot apply $acosh to 0, value must in [1,inf]\",\n\t\"code\" : 50989,\n\t\"codeName\" : \"Location50989\"\n} : aggregate failed :\n_getErrorWithCode@src\/mongo\/shell\/utils.js:25:13\ndoassert@src\/mongo\/shell\/assert.js:18:14\n_assertCommandWorked@src\/mongo\/shell\/assert.js:618:17\nassert.commandWorked@src\/mongo\/shell\/assert.js:708:16\nDB.prototype._runAggregate@src\/mongo\/shell\/db.js:266:5\nDBCollection.prototype.aggregate@src\/mongo\/shell\/collection.js:1046:12\n@(shell):1:1<\/pre>\n\n\n\n<p>As the error message indicates, the value must be between <code>1<\/code> and <code>+Infinity<\/code>.<\/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>$acosh<\/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>$acos<\/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: { $acosh: \"$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&nbsp;<code>NaN<\/code>,&nbsp;<code>$acosh<\/code>&nbsp;returns&nbsp;<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: { $acosh: 1 * \"string\" }\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<p>In this case I tried to multiple a number by a string, which resulted in <code>NaN<\/code> being returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Infinity<\/h2>\n\n\n\n<p>If the argument resolves to <code>Infinity<\/code>, the <code>$acosh<\/code> operator returns <code>Infinity<\/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: { $acosh: Infinity }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"result\" : Infinity }<\/pre>\n\n\n\n<p>However, <code>-Infinity<\/code> will return an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Fields<\/h2>\n\n\n\n<p>If the <code>$acosh<\/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: { $acosh: \"$wrongField\" }\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 $acosh aggregation pipeline operator returns the hyperbolic&nbsp;arccosine (inverse hyperbolic&nbsp;cosine) of a value, measured in radians. $acosh accepts any valid expression that resolves to a number between&nbsp;1&nbsp;and&nbsp;+Infinity. The $acosh 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-14975","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\/14975","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=14975"}],"version-history":[{"count":8,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/14975\/revisions"}],"predecessor-version":[{"id":15022,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/14975\/revisions\/15022"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=14975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=14975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=14975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}