MongoDB $sin

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.

Example

Suppose we have a collection called test with the following document:

{ "_id" : 1, "data" : 2 }

We can use the $sin operator to return the sine of the data field:

db.test.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        sine: { $sin: "$data" }
      }
    }
  ]
)

Result:

{ "sine" : 0.9092974268256817 }

Convert to Radians

As mentioned, $sin returns the sine of a value that is measured in radians. If the value is in degrees, you can use the $degreesToRadians operator to convert it to radians.

Example:

db.test.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        sine: { $degreesToRadians: { $sin: "$data" } }
      }
    }
  ]
)

Result:

{ "sine" : 0.015870233978020357 }

128-Bit Decimal Values

By default, the $sin operator returns values as a double, but it can also return values as a 128-bit decimal as long as the expression resolves to a 128-bit decimal value.

Suppose we add the following document to our collection:

{ "_id" : 2, "data" : NumberDecimal("2.1301023541559787031443874490659") }

Let’s run the the $sin operator against that document:

db.test.aggregate(
  [
    { $match: { _id: 2 } },
    { $project: { 
        _id: 0,
        sine: { $sin: "$data" }
      }
    }
  ]
)

Result:

{ "sine" : NumberDecimal("0.8476235356531703096519423201190329") }

The output is 128-bit decimal.

Null Values

Null values return null when using the $sin operator.

Suppose we add the following document to our collection:

{ "_id" : 3, "data" : null }

Let’s run the the $sin operator against that document:

db.test.aggregate(
  [
    { $match: { _id: 3 } },
    { $project: { 
        _id: 0,
        sine: { $sin: "$data" }
      }
    }
  ]
)

Result:

{ "sine" : null }

We can see that the result is null.

NaN Values

If the argument resolves to NaN$sin returns NaN.

Example:

db.test.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        sine: { $sin: 1 * "$data" }
      }
    }
  ]
)

Result:

{ "sine" : NaN }

Non-Existent Fields

If the $sin operator is applied against a field that doesn’t exist, null is returned.

Example:

db.test.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        sine: { $sin: "$name" }
      }
    }
  ]
)

Result:

{ "sine" : null }

Infinity

Providing Infinity or -Infinity will return an out of range error.

Suppose we add the following document to the collection:

{ "_id" : 4, "data" : Infinity }

Let’s run $sin again:

db.test.aggregate(
  [
    { $match: { _id: 4 } },
    { $project: { 
        _id: 0,
        sine: { $sin: "$data" }
      }
    }
  ]
)

Result:

uncaught exception: Error: command failed: {
	"ok" : 0,
	"errmsg" : "cannot apply $sin to inf, value must in (-inf,inf)",
	"code" : 50989,
	"codeName" : "Location50989"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:618:17
assert.commandWorked@src/mongo/shell/assert.js:708:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12
@(shell):1:1