View a markdown version of this page

$stdDevSamp - Amazon DocumentDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

$stdDevSamp

버전 8.0.1에서 새로 추가되었습니다.

Amazon DocumentDB의 $stdDevSamp 연산자는 숫자 값의 샘플 표준 편차를 계산합니다. 누적기로서 집계 파이프라인의 $group 단계에서 그룹 내의 문서 간에 샘플 표준 편차를 계산합니다. 표현식으로 숫자 배열의 샘플 표준 편차를 계산합니다. 샘플 표준 편차는 N-1을 제곱으로 사용합니다(Bessel의 수정). 숫자가 아닌 값은 무시됩니다. 숫자 값이 2개 미만인 경우를 반환합니다null.

파라미터

  • expression: 숫자 값 또는 숫자 값 배열로 확인되는 표현식입니다.

예제(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 Shell)

연$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 Shell)

버전 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 } ]

각 문서는 현재 문서까지 파티션 value 내의 샘플 표준 편차runningStdDev인 로 보강됩니다. 창에 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()