$sqrt - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$sqrt

バージョン 4.0 の新機能。

Amazon DocumentDB の $sqrt演算子は、数値の平方根を計算するために使用されます。

パラメータ

  • expression: 引数は、負以外の数値に解決される限り、任意の有効な式にすることができます。

例 (MongoDB シェル)

次の例は、$sqrt演算子を使用して数値の平方根を計算する方法を示しています。

サンプルドキュメントを作成する

db.numbers.insertMany([ { "_id": 1, "number": 16 }, { "_id": 2, "number": 36 }, { "_id": 3, "number": 64 } ]);

クエリの例

db.numbers.aggregate([ { $project: { "_id": 1, "square_root": { $sqrt: "$number" } }} ]);

出力

[ { _id: 1, square_root: 4 }, { _id: 2, square_root: 6 }, { _id: 3, square_root: 8 } ]

コードの例

$sqrt コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

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()