翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$stdDevSamp
バージョン 8.0.1 から新規。
Amazon DocumentDB の $stdDevSamp演算子は、数値のサンプル標準偏差を計算します。アキュムレータは、集約パイプラインの$groupステージのグループ内のドキュメント全体のサンプル標準偏差を計算します。式として、数値の配列のサンプル標準偏差を計算します。サンプル標準偏差は、N-1 を除数として使用します (Bessel の補正)。数値以外の値は無視されます。数値が 2 つ未満の場合は、 を返しますnull。
パラメータ
例 (MongoDB シェル)
次の例は、 $stdDevSamp演算子を使用して、サブジェクトあたりのスコアのサンプル標準偏差を計算する方法を示しています。
サンプルドキュメントを作成する
db.scores.insertMany([
{ subject: "math", score: 80 },
{ subject: "math", score: 90 },
{ subject: "math", score: 85 },
{ subject: "math", score: 95 },
{ subject: "science", score: 70 },
{ subject: "science", score: 75 },
{ subject: "science", score: 80 },
{ subject: "science", score: 85 }
]);
クエリの例
db.scores.aggregate([
{ $group: {
_id: "$subject",
stdDev: { $stdDevSamp: "$score" }
}}
]);
出力
[
{ "_id": "math", "stdDev": 6.454972243679028 },
{ "_id": "science", "stdDev": 6.454972243679028 }
]
式の使用例 (MongoDB シェル)
$stdDevSamp 演算子を$projectステージ内の式として使用して、配列フィールドのサンプル標準偏差を計算することもできます。
サンプルドキュメントを作成する
db.experiments.insertMany([
{ _id: 1, measurements: [10, 12, 14, 16, 18] },
{ _id: 2, measurements: [5, 5, 5, 5, 5] },
{ _id: 3, measurements: [2, 4, 6, 8, 10] }
]);
クエリの例
db.experiments.aggregate([
{ $project: {
stdDev: { $stdDevSamp: "$measurements" }
}}
]);
出力
[
{ "_id": 1, "stdDev": 3.1622776601683795 },
{ "_id": 2, "stdDev": 0 },
{ "_id": 3, "stdDev": 3.1622776601683795 }
]
コードの例
$stdDevSamp 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。次の例は、アキュムレータの使用 ( の場合$group) と式の使用 ( の場合) の両方を示しています$project。
- Node.js
-
const { MongoClient } = require('mongodb');
async function example() {
const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false';
const client = new MongoClient(uri);
try {
await client.connect();
const db = client.db('test');
// Accumulator usage: stdDevSamp across grouped documents
const scores = db.collection('scores');
const accumulatorResult = await scores.aggregate([
{ $group: {
_id: "$subject",
stdDev: { $stdDevSamp: "$score" }
}}
]).toArray();
console.log('Accumulator result:', accumulatorResult);
// Expression usage: stdDevSamp of an array field
const experiments = db.collection('experiments');
const expressionResult = await experiments.aggregate([
{ $project: {
stdDev: { $stdDevSamp: "$measurements" }
}}
]).toArray();
console.log('Expression result:', expressionResult);
} finally {
await client.close();
}
}
example();
- 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']
# Accumulator usage: stdDevSamp across grouped documents
scores = db['scores']
accumulator_result = list(scores.aggregate([
{ '$group': {
'_id': '$subject',
'stdDev': { '$stdDevSamp': '$score' }
}}
]))
print('Accumulator result:', accumulator_result)
# Expression usage: stdDevSamp of an array field
experiments = db['experiments']
expression_result = list(experiments.aggregate([
{ '$project': {
'stdDev': { '$stdDevSamp': '$measurements' }
}}
]))
print('Expression result:', expression_result)
finally:
client.close()
example()