{"id":15071,"date":"2021-02-28T09:32:46","date_gmt":"2021-02-27T23:32:46","guid":{"rendered":"https:\/\/database.guide\/?p=15071"},"modified":"2021-02-28T09:32:48","modified_gmt":"2021-02-27T23:32:48","slug":"mongodb-sin","status":"publish","type":"post","link":"https:\/\/database.guide\/mongodb-sin\/","title":{"rendered":"MongoDB $sin"},"content":{"rendered":"\n<p>In MongoDB, the <code>$sin<\/code> aggregation pipeline operator returns the sine of a value that is measured in radians.<\/p>\n\n\n\n<p><code>$sin<\/code> accepts any valid expression that resolves to a number.<\/p>\n\n\n\n<p>The <code>$sin<\/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>$sin<\/code> operator to return the sine 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        sine: { $sin: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"sine\" : 0.9092974268256817 }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Convert to Radians<\/h2>\n\n\n\n<p>As mentioned, <code>$sin<\/code> returns the sine of a value that is measured in radians. If the value is in degrees, you can use the <code><a href=\"https:\/\/database.guide\/mongodb-degreestoradians\/\" title=\"MongoDB $degreesToRadians\">$degreesToRadians<\/a><\/code> operator to convert it 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        sine: { $degreesToRadians: { $sin: \"$data\" } }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"sine\" : 0.015870233978020357 }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">128-Bit Decimal Values<\/h2>\n\n\n\n<p>By default, the&nbsp;<code>$sin<\/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>$sin<\/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        sine: { $sin: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"sine\" : NumberDecimal(\"0.8476235356531703096519423201190329\") }<\/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>$sin<\/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>$sin<\/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        sine: { $sin: \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"sine\" : 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>$sin<\/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: 1 } },\n    { $project: { \n        _id: 0,\n        sine: { $sin: 1 * \"$data\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"sine\" : NaN }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Existent Fields<\/h2>\n\n\n\n<p>If the <code>$sin<\/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: 1 } },\n    { $project: { \n        _id: 0,\n        sine: { $sin: \"$name\" }\n      }\n    }\n  ]\n)<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ \"sine\" : null }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Infinity<\/h2>\n\n\n\n<p>Providing <code>Infinity<\/code> or <code>-Infinity<\/code> will return an out of range 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\" : 4, \"data\" : Infinity }<\/pre>\n\n\n\n<p>Let&#8217;s run <code>$sin<\/code> again:<\/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        sine: { $sin: \"$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 $sin to inf, value must in (-inf,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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MongoDB, the $sin aggregation pipeline operator returns the sine of a value that is measured in radians. $sin accepts any valid expression that resolves to a number. The $sin 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-15071","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\/15071","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=15071"}],"version-history":[{"count":7,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/15071\/revisions"}],"predecessor-version":[{"id":15097,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/15071\/revisions\/15097"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=15071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=15071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=15071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}