MongoDB $cos

In MongoDB, the $cos aggregation pipeline operator returns the cosine of a value that is measured in radians.

$cos accepts any valid expression that resolves to a number.

The $cos operator was introduced in MongoDB 4.2.

Example

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

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

We can use the $cos operator to return the cosine of the data field:

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

Result:

{ "cosine" : -0.9899924966004454 }

By default, the $cos 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.

When the Expression is in Degrees

As mentioned, $cos accepts its expression in radians. You can use the $degreesToRadians operator to convert any values from degrees to radians.

Example:

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

Result:

{ "cosine" : -0.017278628635716543 }

Null Values

Null values return null when using the $cos operator.

Suppose we add the following document to our collection:

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

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

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

Result:

{ "cosine" : null }

We can see that the result is null.

NaN Values

If the argument resolves to NaN$cos returns NaN.

Example:

db.test.aggregate(
  [
    { $match: { _id: 3 } },
    { $project: { 
        _id: 0,
        cosine: { $cos: 1 * "string" }
      }
    }
  ]
)

Result:

{ "cosine" : NaN }

In this case I tried to multiple a number by a string, which resulted in NaN being returned.

Infinity

If the argument resolves to Infinity or -Infinity, the $cos operator returns an error.

Suppose we add the following document to our collection:

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

Let’s run $cos against the data field:

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

Result:

uncaught exception: Error: command failed: {
	"ok" : 0,
	"errmsg" : "cannot apply $cos 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

That’s the error I get in the mongo shell.

Non-Existent Fields

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

Example:

db.test.aggregate(
  [
    { $match: { _id: 4 } },
    { $project: { 
        _id: 0,
        cosine: { $cos: "$carrots" }
      }
    }
  ]
)

Result:

{ "cosine" : null }

128-bit Decimal

As mentioned, if the expression provided to $cos is 128-bit decimal, then the result is returned in 128-bit decimal.

Suppose we add the following document to the collection:

{ "_id" : 5, "data" : NumberDecimal("1.1301023541559787031443874490659") }

Here’s what happens when we run that through the $cos operator:

db.test.aggregate(
  [
    { $match: { _id: 5 } },
    { $project: { 
        _id: 0,
        cosine: { $cos: "$data" }
      }
    }
  ]
)

Result:

{ "cosine" : NumberDecimal("0.4265672353490945266548815934449746") }

The output is 128-bit decimal.