$sqrt

The `$sqrt` operator is used to calculate the square root of a specified number.

Syntax

{ $sqrt: <expression> }

Parameters

<expression>stringrequired

Any valid expression that resolves to a number.

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "sales": { "revenue": 75670 }
}

Calculate the square root of sales

Calculate the square root of the sales volumes for each store.

Query:

db.stores.aggregate([
  { $match: { company: { $in: ["First Up Consultants"] } } },
  {
    $project: {
      name: 1,
      "sales.revenue": 1,
      sqrtFullSales: { $sqrt: "$sales.revenue" }
    }
  }
])

Output:

[
  {
    "_id": "c52c9f65-5b1a-4ef5-a7a2-d1af0426cbe4",
    "name": "First Up Consultants | Jewelry Pantry - Nicolasberg",
    "sales": { "revenue": 4624 },
    "sqrtFullSales": 68
  }
]