翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$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 }
]
ウィンドウ演算子の使用例 (MongoDB シェル)
バージョン 8.0.2 の新機能。
$stdDevSamp 演算子は、$setWindowFieldsステージのウィンドウ演算子としても使用できます。このコンテキストでは、各ウィンドウのドキュメントに対して指定された式のサンプル標準偏差を返します。output フィールドで 演算子を指定し、オプションでwindowドキュメントを使用してウィンドウの境界を定義します。
サンプルドキュメントを作成する
db.sensorReadings.insertMany([
{ _id: 1, sensor: "A", time: 1, value: 2 },
{ _id: 2, sensor: "A", time: 2, value: 4 },
{ _id: 3, sensor: "A", time: 3, value: 6 },
{ _id: 4, sensor: "B", time: 1, value: 10 },
{ _id: 5, sensor: "B", time: 2, value: 12 }
]);
クエリの例
次の例ではsensor、ドキュメントを でパーティション化し、各パーティションを でソートしてtime、パーティションの開始valueから現在のドキュメントまでの実行中のサンプル標準偏差を返します。
db.sensorReadings.aggregate([
{
$setWindowFields: {
partitionBy: "$sensor",
sortBy: { time: 1 },
output: {
runningStdDev: {
$stdDevSamp: "$value",
window: { documents: ["unbounded", "current"] }
}
}
}
}
]);
出力
[
{ "_id": 1, "sensor": "A", "time": 1, "value": 2, "runningStdDev": null },
{ "_id": 2, "sensor": "A", "time": 2, "value": 4, "runningStdDev": 1.4142135623730951 },
{ "_id": 3, "sensor": "A", "time": 3, "value": 6, "runningStdDev": 2 },
{ "_id": 4, "sensor": "B", "time": 1, "value": 10, "runningStdDev": null },
{ "_id": 5, "sensor": "B", "time": 2, "value": 12, "runningStdDev": 1.4142135623730951 }
]
各ドキュメントはrunningStdDev、現在のドキュメントまでのパーティションvalue内の のサンプル標準偏差である で拡張されます。ウィンドウに含まれる値が 2 つ未満の場合 (各パーティションの最初のドキュメント)、 は $stdDevSampを返しますnull。
コードの例
$stdDevSamp 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。次の例は、アキュムレータの使用 ( の場合$group)、式の使用 ( の場合$project)、ウィンドウ演算子の使用 ( の場合) を示しています$setWindowFields。
- 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');
await 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 }
]);
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');
await 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] }
]);
const expressionResult = await experiments.aggregate([
{ $project: {
stdDev: { $stdDevSamp: "$measurements" }
}}
]).toArray();
console.log('Expression result:', expressionResult);
// Window operator usage: running stdDevSamp within each partition
const sensorReadings = db.collection('sensorReadings');
await sensorReadings.insertMany([
{ _id: 1, sensor: "A", time: 1, value: 2 },
{ _id: 2, sensor: "A", time: 2, value: 4 },
{ _id: 3, sensor: "A", time: 3, value: 6 },
{ _id: 4, sensor: "B", time: 1, value: 10 },
{ _id: 5, sensor: "B", time: 2, value: 12 }
]);
const windowResult = await sensorReadings.aggregate([
{
$setWindowFields: {
partitionBy: "$sensor",
sortBy: { time: 1 },
output: {
runningStdDev: {
$stdDevSamp: "$value",
window: { documents: ["unbounded", "current"] }
}
}
}
}
]).toArray();
console.log('Window result:', windowResult);
} 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']
scores.insert_many([
{ '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 }
])
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']
experiments.insert_many([
{ '_id': 1, 'measurements': [10, 12, 14, 16, 18] },
{ '_id': 2, 'measurements': [5, 5, 5, 5, 5] },
{ '_id': 3, 'measurements': [2, 4, 6, 8, 10] }
])
expression_result = list(experiments.aggregate([
{ '$project': {
'stdDev': { '$stdDevSamp': '$measurements' }
}}
]))
print('Expression result:', expression_result)
# Window operator usage: running stdDevSamp within each partition
sensor_readings = db['sensorReadings']
sensor_readings.insert_many([
{ '_id': 1, 'sensor': 'A', 'time': 1, 'value': 2 },
{ '_id': 2, 'sensor': 'A', 'time': 2, 'value': 4 },
{ '_id': 3, 'sensor': 'A', 'time': 3, 'value': 6 },
{ '_id': 4, 'sensor': 'B', 'time': 1, 'value': 10 },
{ '_id': 5, 'sensor': 'B', 'time': 2, 'value': 12 }
])
window_result = list(sensor_readings.aggregate([
{
'$setWindowFields': {
'partitionBy': '$sensor',
'sortBy': { 'time': 1 },
'output': {
'runningStdDev': {
'$stdDevSamp': '$value',
'window': { 'documents': ['unbounded', 'current'] }
}
}
}
}
]))
print('Window result:', window_result)
finally:
client.close()
example()