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

코드 예제

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