View a markdown version of this page

$push - Amazon DocumentDB

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

$push

$push 集計演算子は、各グループの指定された式からすべての値の配列を返します。これは通常、値を配列に蓄積するために$groupステージ内で使用されます。

パラメータ

  • expression: グループ内の各ドキュメントについて評価する式。

例 (MongoDB シェル)

次の例は、 $push演算子を使用して各カテゴリのすべての製品名を収集する方法を示しています。

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

db.sales.insertMany([ { _id: 1, category: "Electronics", product: "Laptop", amount: 1200 }, { _id: 2, category: "Electronics", product: "Mouse", amount: 25 }, { _id: 3, category: "Furniture", product: "Desk", amount: 350 }, { _id: 4, category: "Furniture", product: "Chair", amount: 150 }, { _id: 5, category: "Electronics", product: "Keyboard", amount: 75 } ]);

クエリの例

db.sales.aggregate([ { $group: { _id: "$category", products: { $push: "$product" } } } ]);

出力

[ { _id: 'Furniture', products: [ 'Desk', 'Chair' ] }, { _id: 'Electronics', products: [ 'Laptop', 'Mouse', 'Keyboard' ] } ]

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

バージョン 8.0.2 の新機能。

$push 演算子は、$setWindowFieldsステージのウィンドウ演算子としても使用できます。このコンテキストでは、各ウィンドウのドキュメントに対して指定された式のすべての値の配列をソート順に返します。output フィールドで 演算子を指定し、オプションでwindowドキュメントを使用してウィンドウの境界を定義します。

注記

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

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

db.purchases.insertMany([ { _id: 1, category: "Electronics", day: 1, product: "Laptop" }, { _id: 2, category: "Electronics", day: 2, product: "Mouse" }, { _id: 3, category: "Electronics", day: 3, product: "Keyboard" }, { _id: 4, category: "Furniture", day: 1, product: "Desk" }, { _id: 5, category: "Furniture", day: 2, product: "Chair" } ]);

クエリの例

次の例では、ドキュメントを でパーティション化しcategory、各パーティションを でソートしてday、パーティションの先頭productから現在のドキュメントまでの値の実行リストを返します。

db.purchases.aggregate([ { $setWindowFields: { partitionBy: "$category", sortBy: { day: 1 }, output: { productsSoFar: { $push: "$product", window: { documents: ["unbounded", "current"] } } } } } ]);

出力

[ { "_id": 1, "category": "Electronics", "day": 1, "product": "Laptop", "productsSoFar": [ "Laptop" ] }, { "_id": 2, "category": "Electronics", "day": 2, "product": "Mouse", "productsSoFar": [ "Laptop", "Mouse" ] }, { "_id": 3, "category": "Electronics", "day": 3, "product": "Keyboard", "productsSoFar": [ "Laptop", "Mouse", "Keyboard" ] }, { "_id": 4, "category": "Furniture", "day": 1, "product": "Desk", "productsSoFar": [ "Desk" ] }, { "_id": 5, "category": "Furniture", "day": 2, "product": "Chair", "productsSoFar": [ "Desk", "Chair" ] } ]

各ドキュメントはproductsSoFar、パーティション内のすべてのproduct値の配列である で、最初から現在のドキュメントまでのソート順で拡張されます。

コードの例

$push 演算子を使用するコード例を表示するには、使用する言語のタブを選択します。次の例は、アキュムレータの使用 ( の場合$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: collect all values per group const sales = db.collection('sales'); await sales.insertMany([ { _id: 1, category: "Electronics", product: "Laptop", amount: 1200 }, { _id: 2, category: "Electronics", product: "Mouse", amount: 25 }, { _id: 3, category: "Furniture", product: "Desk", amount: 350 }, { _id: 4, category: "Furniture", product: "Chair", amount: 150 }, { _id: 5, category: "Electronics", product: "Keyboard", amount: 75 } ]); const accumulatorResult = await sales.aggregate([ { $group: { _id: "$category", products: { $push: "$product" } } } ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Window operator usage: running list of values within each partition const purchases = db.collection('purchases'); await purchases.insertMany([ { _id: 1, category: "Electronics", day: 1, product: "Laptop" }, { _id: 2, category: "Electronics", day: 2, product: "Mouse" }, { _id: 3, category: "Electronics", day: 3, product: "Keyboard" }, { _id: 4, category: "Furniture", day: 1, product: "Desk" }, { _id: 5, category: "Furniture", day: 2, product: "Chair" } ]); const windowResult = await purchases.aggregate([ { $setWindowFields: { partitionBy: "$category", sortBy: { day: 1 }, output: { productsSoFar: { $push: "$product", window: { documents: ["unbounded", "current"] } } } } } ]).toArray(); console.log('Window result:', windowResult); 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') db = client['test'] # Accumulator usage: collect all values per group sales = db['sales'] sales.insert_many([ { '_id': 1, 'category': 'Electronics', 'product': 'Laptop', 'amount': 1200 }, { '_id': 2, 'category': 'Electronics', 'product': 'Mouse', 'amount': 25 }, { '_id': 3, 'category': 'Furniture', 'product': 'Desk', 'amount': 350 }, { '_id': 4, 'category': 'Furniture', 'product': 'Chair', 'amount': 150 }, { '_id': 5, 'category': 'Electronics', 'product': 'Keyboard', 'amount': 75 } ]) accumulator_result = list(sales.aggregate([ { '$group': { '_id': '$category', 'products': { '$push': '$product' } } } ])) print('Accumulator result:', accumulator_result) # Window operator usage: running list of values within each partition purchases = db['purchases'] purchases.insert_many([ { '_id': 1, 'category': 'Electronics', 'day': 1, 'product': 'Laptop' }, { '_id': 2, 'category': 'Electronics', 'day': 2, 'product': 'Mouse' }, { '_id': 3, 'category': 'Electronics', 'day': 3, 'product': 'Keyboard' }, { '_id': 4, 'category': 'Furniture', 'day': 1, 'product': 'Desk' }, { '_id': 5, 'category': 'Furniture', 'day': 2, 'product': 'Chair' } ]) window_result = list(purchases.aggregate([ { '$setWindowFields': { 'partitionBy': '$category', 'sortBy': { 'day': 1 }, 'output': { 'productsSoFar': { '$push': '$product', 'window': { 'documents': ['unbounded', 'current'] } } } } } ])) print('Window result:', window_result) client.close() example()