View a markdown version of this page

$minN - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$minN

バージョン 8.0.1 から新規。

Amazon DocumentDB の $minN演算子は、N 個の最小値を返します。$group ステージでアキュムレータとして使用すると、各グループの N 最小値の配列が返されます。配列式演算子として使用すると、配列の N 個の最小要素が返されます。

パラメータ

  • input: n最小値を返す配列またはフィールドに解決される式。

  • n: 返す最小値の数を指定する正の整数に解決される式。

動作

配列式演算子の動作

  • よりn小さい値を指定することはできません1

  • $minN は、input配列で見つかったnull値をフィルタリングします。

  • 指定された ninput配列内の要素の数以上である場合、 はinput配列内のすべての要素$minNを返します。

  • が非配列値にinput解決された場合、集約オペレーションはエラーになります。

  • に数値要素と文字列要素の両方inputが含まれている場合、数値要素は BSON 比較順序に従って文字列要素の前にソートされます。

アキュムレータの動作

  • アキュムレータとして使用する場合、 は定数であるか、 _idの値に依存する正の整数式nである必要があります$group

  • $minN は null 値と欠落した値をフィルタリングします。

  • グループに含まれるn要素が より少ない場合、 はグループ内のすべての要素$minNを返します。

  • $minN は BSON 比較順序に従って入力データを比較し、適切な出力タイプを決定します。入力データに複数のデータ型が含まれている場合、$minN出力型は比較順序で最低になります。

出力順序

$minN は、特定のソート順序で値を返します。特定のソート順序を保証する必要がある場合は、$bottomN代わりに を使用するか、結果を でラップします$sortArray

例 (MongoDB シェル)

次の例は、アキュムレータを使用して各サブジェクトの 2 $minN つの最低スコアを取得する方法を示しています。

サンプルドキュメントを作成する

db.scores.insertMany([ { subject: "math", score: 85 }, { subject: "math", score: 72 }, { subject: "math", score: 93 }, { subject: "math", score: 68 }, { subject: "science", score: 90 }, { subject: "science", score: 78 }, { subject: "science", score: 65 }, { subject: "science", score: 88 } ]);

クエリの例

db.scores.aggregate([ { $group: { _id: "$subject", lowestTwo: { $minN: { input: "$score", n: 2 } } } } ]);

出力

[ { "_id": "math", "lowestTwo": [72, 68] }, { "_id": "science", "lowestTwo": [78, 65] } ]

式の使用例 (MongoDB シェル)

$minN 演算子を$projectステージ内の式として使用して、配列フィールドから N 最小要素を返すこともできます。

サンプルドキュメントを作成する

db.readings.insertMany([ { _id: 1, sensor: "A", values: [45, 12, 78, 3, 56] }, { _id: 2, sensor: "B", values: [90, 23, 67, 11, 44] } ]);

クエリの例

db.readings.aggregate([ { $project: { lowestThree: { $minN: { input: "$values", n: 3 } } }} ]);

出力

[ { "_id": 1, "lowestThree": [45, 12, 3] }, { "_id": 2, "lowestThree": [44, 23, 11] } ]

コードの例

$minN 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。次の例は、アキュムレータの使用 ( の場合$group) と式の使用 ( の場合) の両方を示しています$project

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); // Accumulator usage: N smallest values per group const scores = db.collection('scores'); const accumulatorResult = await scores.aggregate([ { $group: { _id: "$subject", lowestTwo: { $minN: { input: "$score", n: 2 } } } } ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Expression usage: N smallest elements from an array field const readings = db.collection('readings'); const expressionResult = await readings.aggregate([ { $project: { lowestThree: { $minN: { input: "$values", n: 3 } } } } ]).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: N smallest values per group scores = db['scores'] accumulator_result = list(scores.aggregate([ { '$group': { '_id': '$subject', 'lowestTwo': { '$minN': { 'input': '$score', 'n': 2 } } } } ])) print('Accumulator result:', accumulator_result) # Expression usage: N smallest elements from an array field readings = db['readings'] expression_result = list(readings.aggregate([ { '$project': { 'lowestThree': { '$minN': { 'input': '$values', 'n': 3 } } } } ])) print('Expression result:', expression_result) finally: client.close() example()