Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
$push
The $push aggregation operator returns an array of all values from a specified expression for each group. It is typically used within the $group stage to accumulate values into an array.
Parameters
Example (MongoDB Shell)
The following example demonstrates using the $push operator to collect all product names for each category.
Create sample documents
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 }
]);
Query example
db.sales.aggregate([
{
$group: {
_id: "$category",
products: { $push: "$product" }
}
}
]);
Output
[
{ _id: 'Furniture', products: [ 'Desk', 'Chair' ] },
{ _id: 'Electronics', products: [ 'Laptop', 'Mouse', 'Keyboard' ] }
]
Window operator usage example (MongoDB Shell)
New from version 8.0.2.
The $push operator can also be used as a window operator in the $setWindowFields stage. In this context, it returns an array of all values of the specified expression for the documents in each window, in sort order. You specify the operator under the output field, and optionally define the window boundaries with a window document.
When used as a window operator in $setWindowFields, $push is limited to 100 MB of intermediate data. An operation that exceeds this limit returns an error.
Create sample documents
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" }
]);
Query example
The following example partitions the documents by category, sorts each partition by day, and returns a running list of product values from the start of the partition through the current document.
db.purchases.aggregate([
{
$setWindowFields: {
partitionBy: "$category",
sortBy: { day: 1 },
output: {
productsSoFar: {
$push: "$product",
window: { documents: ["unbounded", "current"] }
}
}
}
}
]);
Output
[
{ "_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" ] }
]
Each document is augmented with productsSoFar, the array of all product values within its partition from the start through the current document, in sort order.
Code examples
To view a code example for using the $push operator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in $group) and window operator usage (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()