Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
$sqrt
New from version 4.0.
The $sqrt operator in Amazon DocumentDB is used to calculate the square root of a number.
Parameters
Example (MongoDB Shell)
The following example demonstrates the usage of the $sqrt operator to calculate the square root of a number.
Create sample documents
db.numbers.insertMany([
{ "_id": 1, "number": 16 },
{ "_id": 2, "number": 36 },
{ "_id": 3, "number": 64 }
]);
Query example
db.numbers.aggregate([
{ $project: {
"_id": 1,
"square_root": { $sqrt: "$number" }
}}
]);
Output
[
{ _id: 1, square_root: 4 },
{ _id: 2, square_root: 6 },
{ _id: 3, square_root: 8 }
]
Code examples
To view a code example for using the $sqrt command, choose the tab for the language that you want to use:
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
try {
await client.connect();
const db = client.db('test');
const collection = db.collection('numbers');
const pipeline = [
{
$project: {
_id: 1,
square_root: { $sqrt: '$number' }
}
}
];
const results = await collection.aggregate(pipeline).toArray();
console.dir(results, { depth: null });
} finally {
await client.close();
}
}
example().catch(console.error);
- Python
-
from pymongo import MongoClient
def example():
client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
try:
db = client.test
collection = db.numbers
pipeline = [
{
"$project": {
"_id": 1,
"square_root": {
"$sqrt": "$number"
}
}
}
]
results = collection.aggregate(pipeline)
for doc in results:
print(doc)
except Exception as e:
print(f"An error occurred: {e}")
finally:
client.close()
example()