View a markdown version of this page

$push - Amazon DocumentDB

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

$push

Der $push Aggregationsoperator gibt ein Array aller Werte aus einem angegebenen Ausdruck für jede Gruppe zurück. Er wird in der Regel innerhalb der $group Phase verwendet, um Werte in einem Array zu akkumulieren.

Parameter

  • expression: Der Ausdruck, der für jedes Dokument in der Gruppe ausgewertet werden soll.

Beispiel (MongoDB Shell)

Das folgende Beispiel zeigt, wie der $push Operator verwendet wird, um alle Produktnamen für jede Kategorie zu sammeln.

Erstellen Sie Beispieldokumente

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 } ]);

Beispiel für eine Abfrage

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

Ausgabe

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

Beispiel für die Verwendung von Fensteroperatoren (MongoDB Shell)

Neu ab Version 8.0.2.

Der $push Operator kann auch als Fensterbediener auf der $setWindowFields Bühne verwendet werden. In diesem Zusammenhang gibt es ein Array mit allen Werten des angegebenen Ausdrucks für die Dokumente in jedem Fenster in der Sortierreihenfolge zurück. Sie geben den Operator unter dem output Feld an und definieren optional die Fenstergrenzen mit einem window Dokument.

Anmerkung

Bei Verwendung als Fensteroperator in $setWindowFields $push ist er auf 100 MB an Zwischendaten begrenzt. Eine Operation, die dieses Limit überschreitet, gibt einen Fehler zurück.

Erstellen Sie Beispieldokumente

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" } ]);

Beispiel für eine Abfrage

Das folgende Beispiel partitioniert die Dokumente nachcategory, sortiert jede Partition nach day und gibt eine fortlaufende Liste von product Werten vom Anfang der Partition bis zum aktuellen Dokument zurück.

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

Ausgabe

[ { "_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" ] } ]

Jedes Dokument wird um das Array aller product Werte innerhalb seiner Partition vom Anfang bis zum aktuellen Dokument in der Sortierreihenfolge erweitert. productsSoFar

Codebeispiele

Um ein Codebeispiel für die Verwendung des $push Operators anzuzeigen, wählen Sie die Registerkarte für die Sprache, die Sie verwenden möchten. Die folgenden Beispiele zeigen sowohl die Verwendung des Akkumulators (in$group) als auch die Verwendung von Fensteroperatoren (in$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()