翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$percentile
バージョン 8.0.1 の新機能。
Amazon DocumentDB の $percentile演算子は、数値データの指定されたパーセンタイル値を計算します。アキュムレータは、集約パイプラインの$groupステージのグループ内のドキュメント全体のパーセンタイル値を計算します。式として、数値の配列のパーセンタイルを計算します。
パラメータ
-
input: 数値または数値の配列に解決される式。
-
p: 0 から 1 までのパーセンタイル値の配列。各値は計算するパーセンタイルを表します。たとえば、 は 25 パーセンタイル、50 パーセンタイル、75 パーセンタイルを[0.25, 0.5, 0.75]計算します。
-
method: 計算方法を指定する文字列。現在は、"approximate" のみがサポートされます。
動作
"approximate" メソッドは、t-digest アルゴリズムを使用して、近似パーセンタイルベースのメトリクスを計算します。小さなデータセットでは、個別のパーセンタイル値が同じ結果に解決される場合があります。たとえば、グループごとに 5 つの値しかない場合、p90 と p99 はどちらもグループ内の最大値を返す可能性があります。データポイントの数が増えると、精度が向上します。
例 (MongoDB シェル)
次の例は、 $percentile演算子を使用して、クラスあたりのスコアの 25 パーセンタイルと 75 パーセンタイルを計算する方法を示しています。
サンプルドキュメントを作成する
db.students.insertMany([
{ class: "A", score: 72 },
{ class: "A", score: 85 },
{ class: "A", score: 90 },
{ class: "A", score: 68 },
{ class: "A", score: 95 },
{ class: "B", score: 80 },
{ class: "B", score: 75 },
{ class: "B", score: 92 },
{ class: "B", score: 88 },
{ class: "B", score: 70 }
]);
クエリの例
db.students.aggregate([
{ $group: {
_id: "$class",
percentiles: { $percentile: { input: "$score", p: [0.25, 0.75], method: "approximate" } }
}}
]);
出力
[
{ "_id": "A", "percentiles": [72, 90] },
{ "_id": "B", "percentiles": [75, 88] }
]
式の使用例 (MongoDB シェル)
$percentile 演算子を$projectステージ内の式として使用して、配列フィールドのパーセンタイルを計算することもできます。
サンプルドキュメントを作成する
db.surveys.insertMany([
{ _id: 1, responses: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] },
{ _id: 2, responses: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] }
]);
クエリの例
db.surveys.aggregate([
{ $project: {
quartiles: { $percentile: { input: "$responses", p: [0.25, 0.5, 0.75], method: "approximate" } }
}}
]);
出力
[
{ "_id": 1, "quartiles": [6, 10, 16] },
{ "_id": 2, "quartiles": [5, 9, 15] }
]
ウィンドウ演算子の使用例 (MongoDB シェル)
バージョン 8.0.2 の新機能。
$percentile 演算子は、$setWindowFieldsステージのウィンドウ演算子としても使用できます。このコンテキストでは、各ウィンドウのドキュメントに指定されたパーセンタイル値を計算します。output フィールドで 演算子を指定し、オプションでwindowドキュメントを使用してウィンドウの境界を定義します。
でウィンドウ演算子として使用する場合$setWindowFields、 $percentileは 100 MB の中間データに制限されます。この制限を超えるオペレーションはエラーを返します。
サンプルドキュメントを作成する
db.sensorData.insertMany([
{ _id: 1, sensor: "A", reading: 72 },
{ _id: 2, sensor: "A", reading: 85 },
{ _id: 3, sensor: "A", reading: 90 },
{ _id: 4, sensor: "A", reading: 68 },
{ _id: 5, sensor: "A", reading: 95 },
{ _id: 6, sensor: "B", reading: 80 },
{ _id: 7, sensor: "B", reading: 75 },
{ _id: 8, sensor: "B", reading: 92 },
{ _id: 9, sensor: "B", reading: 88 },
{ _id: 10, sensor: "B", reading: 70 }
]);
クエリの例
次の例では、ドキュメントを でパーティションsensor化し、各パーティション内のすべてのドキュメントreadingで の 25 パーセンタイルと 75 パーセンタイルを計算します。
db.sensorData.aggregate([
{
$setWindowFields: {
partitionBy: "$sensor",
output: {
readingPercentiles: {
$percentile: { input: "$reading", p: [0.25, 0.75], method: "approximate" },
window: { documents: ["unbounded", "unbounded"] }
}
}
}
}
]);
出力
[
{ "_id": 1, "sensor": "A", "reading": 72, "readingPercentiles": [72, 90] },
{ "_id": 2, "sensor": "A", "reading": 85, "readingPercentiles": [72, 90] },
{ "_id": 3, "sensor": "A", "reading": 90, "readingPercentiles": [72, 90] },
{ "_id": 4, "sensor": "A", "reading": 68, "readingPercentiles": [72, 90] },
{ "_id": 5, "sensor": "A", "reading": 95, "readingPercentiles": [72, 90] },
{ "_id": 6, "sensor": "B", "reading": 80, "readingPercentiles": [75, 88] },
{ "_id": 7, "sensor": "B", "reading": 75, "readingPercentiles": [75, 88] },
{ "_id": 8, "sensor": "B", "reading": 92, "readingPercentiles": [75, 88] },
{ "_id": 9, "sensor": "B", "reading": 88, "readingPercentiles": [75, 88] },
{ "_id": 10, "sensor": "B", "reading": 70, "readingPercentiles": [75, 88] }
]
各ドキュメントはreadingPercentiles、パーティション内のすべてのドキュメントreadingの のおよそ 25 パーセンタイルと 75 パーセンタイルである で拡張されます。
コードの例
$percentile 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。次の例は、アキュムレータの使用 ( の場合$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: percentiles across grouped documents
const students = db.collection('students');
await students.insertMany([
{ class: "A", score: 72 },
{ class: "A", score: 85 },
{ class: "A", score: 90 },
{ class: "A", score: 68 },
{ class: "A", score: 95 },
{ class: "B", score: 80 },
{ class: "B", score: 75 },
{ class: "B", score: 92 },
{ class: "B", score: 88 },
{ class: "B", score: 70 }
]);
const accumulatorResult = await students.aggregate([
{ $group: {
_id: "$class",
percentiles: { $percentile: { input: "$score", p: [0.25, 0.75], method: "approximate" } }
}}
]).toArray();
console.log('Accumulator result:', accumulatorResult);
// Expression usage: percentiles of an array field
const surveys = db.collection('surveys');
await surveys.insertMany([
{ _id: 1, responses: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] },
{ _id: 2, responses: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] }
]);
const expressionResult = await surveys.aggregate([
{ $project: {
quartiles: { $percentile: { input: "$responses", p: [0.25, 0.5, 0.75], method: "approximate" } }
}}
]).toArray();
console.log('Expression result:', expressionResult);
// Window operator usage: percentiles across each partition
const sensorData = db.collection('sensorData');
await sensorData.insertMany([
{ _id: 1, sensor: "A", reading: 72 },
{ _id: 2, sensor: "A", reading: 85 },
{ _id: 3, sensor: "A", reading: 90 },
{ _id: 4, sensor: "A", reading: 68 },
{ _id: 5, sensor: "A", reading: 95 },
{ _id: 6, sensor: "B", reading: 80 },
{ _id: 7, sensor: "B", reading: 75 },
{ _id: 8, sensor: "B", reading: 92 },
{ _id: 9, sensor: "B", reading: 88 },
{ _id: 10, sensor: "B", reading: 70 }
]);
const windowResult = await sensorData.aggregate([
{
$setWindowFields: {
partitionBy: "$sensor",
output: {
readingPercentiles: {
$percentile: { input: "$reading", p: [0.25, 0.75], method: "approximate" },
window: { documents: ["unbounded", "unbounded"] }
}
}
}
}
]).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: percentiles across grouped documents
students = db['students']
students.insert_many([
{ 'class': 'A', 'score': 72 },
{ 'class': 'A', 'score': 85 },
{ 'class': 'A', 'score': 90 },
{ 'class': 'A', 'score': 68 },
{ 'class': 'A', 'score': 95 },
{ 'class': 'B', 'score': 80 },
{ 'class': 'B', 'score': 75 },
{ 'class': 'B', 'score': 92 },
{ 'class': 'B', 'score': 88 },
{ 'class': 'B', 'score': 70 }
])
accumulator_result = list(students.aggregate([
{ '$group': {
'_id': '$class',
'percentiles': { '$percentile': { 'input': '$score', 'p': [0.25, 0.75], 'method': 'approximate' } }
}}
]))
print('Accumulator result:', accumulator_result)
# Expression usage: percentiles of an array field
surveys = db['surveys']
surveys.insert_many([
{ '_id': 1, 'responses': [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] },
{ '_id': 2, 'responses': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] }
])
expression_result = list(surveys.aggregate([
{ '$project': {
'quartiles': { '$percentile': { 'input': '$responses', 'p': [0.25, 0.5, 0.75], 'method': 'approximate' } }
}}
]))
print('Expression result:', expression_result)
# Window operator usage: percentiles across each partition
sensor_data = db['sensorData']
sensor_data.insert_many([
{ '_id': 1, 'sensor': 'A', 'reading': 72 },
{ '_id': 2, 'sensor': 'A', 'reading': 85 },
{ '_id': 3, 'sensor': 'A', 'reading': 90 },
{ '_id': 4, 'sensor': 'A', 'reading': 68 },
{ '_id': 5, 'sensor': 'A', 'reading': 95 },
{ '_id': 6, 'sensor': 'B', 'reading': 80 },
{ '_id': 7, 'sensor': 'B', 'reading': 75 },
{ '_id': 8, 'sensor': 'B', 'reading': 92 },
{ '_id': 9, 'sensor': 'B', 'reading': 88 },
{ '_id': 10, 'sensor': 'B', 'reading': 70 }
])
window_result = list(sensor_data.aggregate([
{
'$setWindowFields': {
'partitionBy': '$sensor',
'output': {
'readingPercentiles': {
'$percentile': { 'input': '$reading', 'p': [0.25, 0.75], 'method': 'approximate' },
'window': { 'documents': ['unbounded', 'unbounded'] }
}
}
}
}
]))
print('Window result:', window_result)
finally:
client.close()
example()