View a markdown version of this page

$min - Amazon DocumentDB

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

$min

$min 集計演算子は、ドキュメントのセット全体で指定されたフィールドの最小値を返します。この演算子は、一連のドキュメントで最小値を見つけるのに役立ちます。

パラメータ

  • expression: 評価する式。これは、フィールドパス、変数、または値に解決される任意の式です。

例 (MongoDB シェル)

次の例は、 $min演算子を使用して、複数のドキュメントにわたる ageフィールドの最小値を見つける方法を示しています。

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

db.users.insertMany([ { name: "John", age: 35 }, { name: "Jane", age: 28 }, { name: "Bob", age: 42 }, { name: "Alice", age: 31 } ]);

クエリの例

db.users.aggregate([ { $group: { _id: null, minAge: { $min: "$age" } } }, { $project: { _id: 0, minAge: 1 } } ])

出力

[ { minAge: 28 } ]

ウィンドウ演算子の使用例 (MongoDB シェル)

バージョン 8.0.2 の新機能。

$min 演算子は、$setWindowFieldsステージのウィンドウ演算子としても使用できます。このコンテキストでは、各ウィンドウのドキュメントの最小値を返します。output フィールドで 演算子を指定し、オプションでwindowドキュメントを使用してウィンドウの境界を定義します。

注記

でウィンドウ演算子として使用する場合$setWindowFields、 $minは 100 MB の中間データに制限されます。この制限を超えるオペレーションはエラーを返します。

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

db.stockPrices.insertMany([ { _id: 1, ticker: "ABC", hour: 1, price: 50 }, { _id: 2, ticker: "ABC", hour: 2, price: 45 }, { _id: 3, ticker: "ABC", hour: 3, price: 60 }, { _id: 4, ticker: "ABC", hour: 4, price: 40 }, { _id: 5, ticker: "XYZ", hour: 1, price: 30 }, { _id: 6, ticker: "XYZ", hour: 2, price: 35 }, { _id: 7, ticker: "XYZ", hour: 3, price: 25 } ]);

クエリの例

次の例では、ドキュメントを でパーティション化しticker、各パーティションを でソートしてhour、現在のドキュメントを通じてパーティションの先頭から見た最低価格を返します。

db.stockPrices.aggregate([ { $setWindowFields: { partitionBy: "$ticker", sortBy: { hour: 1 }, output: { lowestSoFar: { $min: "$price", window: { documents: ["unbounded", "current"] } } } } } ]);

出力

[ { "_id": 1, "ticker": "ABC", "hour": 1, "price": 50, "lowestSoFar": 50 }, { "_id": 2, "ticker": "ABC", "hour": 2, "price": 45, "lowestSoFar": 45 }, { "_id": 3, "ticker": "ABC", "hour": 3, "price": 60, "lowestSoFar": 45 }, { "_id": 4, "ticker": "ABC", "hour": 4, "price": 40, "lowestSoFar": 40 }, { "_id": 5, "ticker": "XYZ", "hour": 1, "price": 30, "lowestSoFar": 30 }, { "_id": 6, "ticker": "XYZ", "hour": 2, "price": 35, "lowestSoFar": 30 }, { "_id": 7, "ticker": "XYZ", "hour": 3, "price": 25, "lowestSoFar": 25 } ]

各ドキュメントは で拡張されます。これはlowestSoFar、現在のドキュメントまで、パーティションprice内の の実行最小値です。

コードの例

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); // Accumulator usage: minimum value across a group const users = db.collection('users'); await users.insertMany([ { name: "John", age: 35 }, { name: "Jane", age: 28 }, { name: "Bob", age: 42 }, { name: "Alice", age: 31 } ]); const accumulatorResult = await users.aggregate([ { $group: { _id: null, minAge: { $min: "$age" } } } ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Window operator usage: running minimum within each partition const stockPrices = db.collection('stockPrices'); await stockPrices.insertMany([ { _id: 1, ticker: "ABC", hour: 1, price: 50 }, { _id: 2, ticker: "ABC", hour: 2, price: 45 }, { _id: 3, ticker: "ABC", hour: 3, price: 60 }, { _id: 4, ticker: "ABC", hour: 4, price: 40 }, { _id: 5, ticker: "XYZ", hour: 1, price: 30 }, { _id: 6, ticker: "XYZ", hour: 2, price: 35 }, { _id: 7, ticker: "XYZ", hour: 3, price: 25 } ]); const windowResult = await stockPrices.aggregate([ { $setWindowFields: { partitionBy: "$ticker", sortBy: { hour: 1 }, output: { lowestSoFar: { $min: "$price", window: { documents: ["unbounded", "current"] } } } } } ]).toArray(); console.log('Window result:', windowResult); 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') db = client['test'] # Accumulator usage: minimum value across a group users = db.users users.insert_many([ { "name": "John", "age": 35 }, { "name": "Jane", "age": 28 }, { "name": "Bob", "age": 42 }, { "name": "Alice", "age": 31 } ]) accumulator_result = list(users.aggregate([ { "$group": { "_id": None, "minAge": { "$min": "$age" } } } ])) print('Accumulator result:', accumulator_result) # Window operator usage: running minimum within each partition stock_prices = db.stockPrices stock_prices.insert_many([ { "_id": 1, "ticker": "ABC", "hour": 1, "price": 50 }, { "_id": 2, "ticker": "ABC", "hour": 2, "price": 45 }, { "_id": 3, "ticker": "ABC", "hour": 3, "price": 60 }, { "_id": 4, "ticker": "ABC", "hour": 4, "price": 40 }, { "_id": 5, "ticker": "XYZ", "hour": 1, "price": 30 }, { "_id": 6, "ticker": "XYZ", "hour": 2, "price": 35 }, { "_id": 7, "ticker": "XYZ", "hour": 3, "price": 25 } ]) window_result = list(stock_prices.aggregate([ { "$setWindowFields": { "partitionBy": "$ticker", "sortBy": { "hour": 1 }, "output": { "lowestSoFar": { "$min": "$price", "window": { "documents": ["unbounded", "current"] } } } } } ])) print('Window result:', window_result) client.close() example()