View a markdown version of this page

$stdDevPop - Amazon DocumentDB

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

$stdDevPop

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

Amazon DocumentDB의 $stdDevPop 연산자는 숫자 값의 모집단 표준 편차를 계산합니다. 누적기는 집계 파이프라인의 $group 단계에서 그룹 내 문서 전반의 모집단 표준 편차를 계산합니다. 표현식으로 숫자 배열의 모집단 표준 편차를 계산합니다. 모집단 표준 편차는 N을 제곱(N-1 아님)으로 사용합니다. 숫자가 아닌 값은 무시됩니다. 숫자 값이 없으면를 반환합니다null. 숫자 값이 하나만 있는 경우를 반환합니다0.

파라미터

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

예제(MongoDB 쉘)

다음 예제에서는 $stdDevPop 연산자를 사용하여 주제당 점수의 모집단 표준 편차를 계산하는 방법을 보여줍니다.

샘플 문서 생성

db.scores.insertMany([ { subject: "math", score: 60 }, { subject: "math", score: 75 }, { subject: "math", score: 85 }, { subject: "math", score: 92 }, { subject: "math", score: 78 }, { subject: "science", score: 55 }, { subject: "science", score: 70 }, { subject: "science", score: 82 }, { subject: "science", score: 91 }, { subject: "science", score: 67 } ]);

쿼리 예제

db.scores.aggregate([ { $group: { _id: "$subject", stdDev: { $stdDevPop: "$score" } }} ]);

출력

[ { "_id": "math", "stdDev": 10.75174404457249 }, { "_id": "science", "stdDev": 12.441864811996632 } ]

표현식 사용 예제(MongoDB Shell)

$stdDevPop산자를 $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: { $stdDevPop: "$measurements" } }} ]);

출력

[ { "_id": 1, "stdDev": 2.8284271247461903 }, { "_id": 2, "stdDev": 0 }, { "_id": 3, "stdDev": 2.8284271247461903 } ]

코드 예제

$stdDevPop 연산자 사용에 대한 코드 예제를 보려면 사용하려는 언어의 탭을 선택합니다. 다음 예제에서는 누적기 사용량()과 표현식 사용량($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: stdDevPop across grouped documents const scores = db.collection('scores'); const accumulatorResult = await scores.aggregate([ { $group: { _id: "$subject", stdDev: { $stdDevPop: "$score" } }} ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Expression usage: stdDevPop of an array field const experiments = db.collection('experiments'); const expressionResult = await experiments.aggregate([ { $project: { stdDev: { $stdDevPop: "$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: stdDevPop across grouped documents scores = db['scores'] accumulator_result = list(scores.aggregate([ { '$group': { '_id': '$subject', 'stdDev': { '$stdDevPop': '$score' } }} ])) print('Accumulator result:', accumulator_result) # Expression usage: stdDevPop of an array field experiments = db['experiments'] expression_result = list(experiments.aggregate([ { '$project': { 'stdDev': { '$stdDevPop': '$measurements' } }} ])) print('Expression result:', expression_result) finally: client.close() example()