{"id":14993,"date":"2021-02-23T09:29:41","date_gmt":"2021-02-22T23:29:41","guid":{"rendered":"https:\/\/database.guide\/?p=14993"},"modified":"2021-02-23T09:33:06","modified_gmt":"2021-02-22T23:33:06","slug":"mongodb-cosh","status":"publish","type":"post","link":"https:\/\/database.guide\/mongodb-cosh\/","title":{"rendered":"MongoDB $cosh"},"content":{"rendered":"\n<p>In MongoDB, the <code>$cosh<\/code> aggregation pipeline operator returns the hyperbolic cosine of a value that is measured in radians.<\/p>\n\n\n\n<p><code>$cosh<\/code> accepts any valid expression that resolves to a number.<\/p>\n\n\n\n<p>The <code>$cosh<\/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>$cosh<\/code> operator to return the cosine 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        hyperbolicCosine: { $cosh: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicCosine\" : 10.067661995777765 }<\/pre>\n\n\n\n<p>By default, the&nbsp;<code>$cosh<\/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\">When the Expression is in Degrees<\/h2>\n\n\n\n<p>As mentioned, <code>$cosh<\/code> accepts its expression in radians. You can use the <code><a href=\"https:\/\/database.guide\/mongodb-degreestoradians\/\" title=\"MongoDB $degreesToRadians\">$degreesToRadians<\/a><\/code> operator to convert any values from degrees to radians.<\/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        hyperbolicCosine: { $degreesToRadians: { $cosh: \"$data\" } }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicCosine\" : 0.17571384980422547 }<\/pre>\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>$cosh<\/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>$cosh<\/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        hyperbolicCosine: { $cosh: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicCosine\" : 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>$cosh<\/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        hyperbolicCosine: { $cosh: 1 * \"string\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicCosine\" : 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> or <code>-Infinity<\/code>, the <code>$cosh<\/code> operator returns <code>Infinity<\/code>.<\/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\" : 4, \"data\" : Infinity }<\/pre>\n\n\n\n<p>Let&#8217;s run <code>$cosh<\/code> against the data field:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.test.aggregate(\n  &#91;\n    { $match: { _id: 4 } },\n    { $project: { \n        _id: 0,\n        hyperbolicCosine: { $cosh: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicCosine\" : Infinity }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Fields<\/h2>\n\n\n\n<p>If the <code>$cosh<\/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: 4 } },\n    { $project: { \n        _id: 0,\n        hyperbolicCosine: { $cosh: \"$name\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicCosine\" : null }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">128-bit Decimal<\/h2>\n\n\n\n<p>As mentioned, if the expression provided to <code>$cosh<\/code> is 128-bit decimal, then the result is returned in 128-bit decimal.<\/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\" : 5, \"data\" : NumberDecimal(\"1.1301023541559787031443874490659\") }<\/pre>\n\n\n\n<p>Here&#8217;s what happens when we run that through the <code>$cosh<\/code> operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.test.aggregate(\n  &#91;\n    { $match: { _id: 5 } },\n    { $project: { \n        _id: 0,\n        hyperbolicCosine: { $cosh: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"hyperbolicCosine\" : NumberDecimal(\"1.709486781983575502518713909095045\") }<\/pre>\n\n\n\n<p>The output is 128-bit decimal. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MongoDB, the $cosh aggregation pipeline operator returns the hyperbolic cosine of a value that is measured in radians. $cosh accepts any valid expression that resolves to a number. The $cosh 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-14993","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\/14993","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=14993"}],"version-history":[{"count":7,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/14993\/revisions"}],"predecessor-version":[{"id":15021,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/14993\/revisions\/15021"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=14993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=14993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=14993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}