MongoDB $asin Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $asin?

$asin is a MongoDB aggregation expression that returns the inverse sine (arc sine) of a number—the angle whose sine equals that value. Results are in radians. Input must be between -1 and 1 inclusive.

Remember
$asin = Math.asin for aggregation

{ $asin: <expression> }

input  → must be in [-1, 1]
output → radians (-π/2 … π/2)
degrees → wrap with $radiansToDegrees

null / missing → null
outside [-1, 1] → pipeline error
UsePrefer
Angle from a sine value$asin
Radians → degrees$radiansToDegrees
Inverse cosine / sine of a number$acos / $sin

How to Use $asin in MongoDB

Pass a sine value (or a ratio that produces one), then convert units if needed.

  1. Choose an expression stage ($addFields or $project).
  2. Ensure the argument is in [-1, 1] (often opposite / hypotenuse via $divide).
  3. Pass that expression to $asin to get radians.
  4. Optionally wrap with $radiansToDegrees for a degree result.
PieceRequired?Role
$asinYesInverse sine → radians
ExpressionYesNumber in [-1, 1], field, or nested math
$radiansToDegreesNoConvert output when you want degrees

Pseudocode

Pseudocode
for each document:
    x = evaluate(expression)
    if x is null or missing:
        result = null
    else if x is NaN:
        result = NaN
    else if x < -1 or x > 1:
        error  // aggregation fails
    else:
        result = asin(x)   // radians
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing inverse-sine expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $asin: <expression> }
Domain-1 ≤ x ≤ 1
Range-π/2 to π/2 radians
Degrees pattern{ $radiansToDegrees: { $asin: <x> } }
Triangle pattern$asin({ $divide: [opposite, hypotenuse] })
Sample check3-4-5 triangle: asin(3/5) ≈ 36.87°

Live Preview

Watch $asin turn a sine value into radians (and degrees). Try values in [-1, 1].

Simulates $asin then $radiansToDegrees (same idea as the mongosh examples below).

Result
x = 0.6
$asin(x) → 0.643501 rad
$radiansToDegrees → 36.869898 °

{ angle_rad: 0.643501, angle_deg: 36.869898 }

Worked Walkthrough — 3-4-5 Triangle

Right triangle with sides 3, 4, and 5. Angle A is opposite side_a (3), so sine of A = opposite / hypotenuse = 3 / 5.

Calculation
Document:
  { side_a: 3, side_b: 4, hypotenuse: 5 }

sin(A) = opposite / hypotenuse
       = side_a / hypotenuse
       = 3 / 5
       = 0.6

$asin(0.6) ≈ 0.6435 radians

$radiansToDegrees(...) ≈ 36.87 degrees

Check: sin(36.87°) ≈ 0.6 ✓

MongoDB Examples

We use a 3-4-5 right triangle. First we get the angle in radians, then convert to degrees. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one right triangle with sides 3, 4, and 5. We save those three lengths as one document in a collection named trigonometry.

mongosh
db.trigonometry.insertOne({
  _id: 1,
  side_a: 3,
  side_b: 4,
  hypotenuse: 5
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in trigonometry. Run find() to see it:

mongosh
db.trigonometry.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
side_a3Short leg (opposite angle A)
side_b4Longer leg (adjacent to angle A)
hypotenuse5Longest side (opposite the right angle)
Picture
Right triangle (easy 3-4-5):

        |\
        | \
  3     |  \   5  (hypotenuse)
(side_a)|   \
        |    \
        |_____\
           4
        (side_b)

We will find angle A (at the bottom-left corner):
  sin(A) = side opposite A / longest side
         = side_a / hypotenuse
         = 3 / 5
         = 0.6

How It Works

  1. Insert once. insertOne adds a single triangle document to trigonometry.
  2. MongoDB stores fields as written. side_a, side_b, and hypotenuse are just numbers: 3, 4, and 5.
  3. find() shows the same shape. What you inserted is what you get back—ready for $asin in the next examples.

Example 2 — Angle in Radians

Goal: compute angle A in radians using sin(A) = side_a / hypotenuse.

mongosh
db.trigonometry.aggregate([
  {
    $addFields: {
      angle_a_rad: {
        $asin: {
          $divide: ["$side_a", "$hypotenuse"]
        }
      }
    }
  }
]);

How It Works

  1. Read the stored sides. MongoDB already has side_a: 3 and hypotenuse: 5 from Example 1.
  2. Build the sine. $divide: ["$side_a", "$hypotenuse"] → 3 / 5 = 0.6.
  3. Take inverse sine. $asin(0.6) returns the angle in radians (about 0.6435).
  4. Keep all fields. $addFields adds angle_a_rad next to the original sides—nothing is deleted.

Example 3 — Angle in Degrees

Same triangle, but wrap $asin with $radiansToDegrees for a more familiar unit.

mongosh
db.trigonometry.aggregate([
  {
    $addFields: {
      angle_a: {
        $radiansToDegrees: {
          $asin: {
            $divide: ["$side_a", "$hypotenuse"]
          }
        }
      }
    }
  }
]);

How It Works

  1. Same stored document. Still using sides 3, 4, and 5 from Example 1.
  2. Same sine ratio. 3 / 5 = 0.6, then $asin gives radians (~0.6435).
  3. Convert units. $radiansToDegrees turns that into about 36.87°.
  4. Easy check. In a 3-4-5 triangle, the smaller acute angle is about 37°—so 36.87° looks right (same as $acos(4/5)).

Edge Cases & Pitfalls

Check these before using $asin in production pipelines.

domain

Stay inside [-1, 1]

Values outside the domain make the pipeline fail. Clamp or validate ratios before calling $asin.

units

Output is radians

Do not treat the raw result as degrees. Convert with $radiansToDegrees when your report expects °.

null

Null and missing become null

Missing fields return null, not an error. Handle them before trig if nulls are invalid in your data.

hyp

Avoid divide-by-zero

Triangle ratios need a non-zero hypotenuse. Guard with $cond or filter bad rows first.

Performance Notes

ApproachCostBest for
$asin alonePer-document mathAngles already stored as sines
$divide + $asinStill O(docs)Right-triangle angles from sides
+ $radiansToDegreesOne extra conversionHuman-readable degree reports

Trig expressions are cheap compared with large sorts or joins. Filter early with $match when only some documents need an angle.

Key Takeaways

  • Role: inverse sine → angle in radians.
  • Domain: argument must be in [-1, 1].
  • Degrees: wrap with $radiansToDegrees.
  • Triangles: $asin(opposite / hypotenuse).

One line: $asin answers “which angle has this sine?”—in radians, for inputs between -1 and 1.

Frequently Asked Questions

$asin returns the inverse sine (arc sine) of a number—the angle whose sine is that number. Results are in radians. Use it inside aggregation expression stages such as $project and $addFields.
The argument must resolve to a number between -1 and 1 inclusive. Values outside that range cause the aggregation to fail with an error.
$asin returns radians. Convert with $radiansToDegrees, or multiply by 180/π using $multiply and $divide.
$asin returns null when the input is null or the field is missing. If the argument is NaN, $asin returns NaN.
Sine equals opposite / hypotenuse. Divide those sides, then pass the ratio to $asin. Wrap with $radiansToDegrees if you want degrees.
Yes for the math. Think of $asin as MongoDB’s aggregation version of Math.asin—run it in the pipeline, not as a standalone find() filter.

Next: $asinh

Compute the inverse hyperbolic sine in an aggregation pipeline.

$asinh tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful