View a markdown version of this page

$push - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$push

$push聚合运算符返回每个组的指定表达式中所有值的数组。它通常在$group阶段内用于将值累积到数组中。

参数

  • expression:要为组中每个文档计算的表达式。

示例(MongoDB Shell)

以下示例演示使用$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 Shell)

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()