该 $push
运算符用于向文档中的数组添加指定值。 $push运算符将新元素添加到现有数组,而不会影响数组中的其他元素。
语法
db.collection.update({
< query >
}, {
$push: {
< field >: < value >
}
}, {
< options >
})
参数
参数 | 说明 |
---|---|
<query> |
要更新的文档的选择条件。 |
<field> |
将附加值的数组字段。 |
<value> |
要附加到数组字段的值。 |
<options> |
可选。 更新操作的附加选项。 |
例子
请考虑存储集合中的此示例文档。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
示例 1 - 添加新的销售类别
若要将新的销售类别添加到 salesByCategory 数组,请使用字段上的 $push 运算符和具有类别名称及其销售量的新 Sales 对象运行查询。
db.stores.update(
{ _id: "0fcc0bf0-ed18-4ab8-b558-9848e18058f4" },
{ $push: { "sales.salesByCategory": { "categoryName": "Wine Accessories", "totalSales": 1000.00 } } }
)
此查询返回以下结果:
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
]
示例 2 - 将$push与$setWindowFields配合使用
若要检索“第一次启动顾问”公司下所有商店的不同销售量,请先运行查询以对公司内的商店进行分区。 然后,使用 $push 运算符创建从第一个到分区中的当前商店的销售列表。
db.stores.aggregate([{
"$match": {
"company": {
"$in": ["First Up Consultants"]
}
}
}, {
"$setWindowFields": {
"partitionBy": "$company",
"sortBy": {
"sales.totalSales": -1
},
"output": {
"salesByStore": {
"$push": "$sales.totalSales",
"window": {
"documents": ["unbounded", "current"]
}
}
}
}
},
{
"$project": {
"company": 1,
"salesByStore": 1
}
}
])
此查询返回的前三个结果为:
[
{
"_id": "a0386810-b6f8-4b05-9d60-e536fb2b0026",
"company": "First Up Consultants",
"salesByStore": [
327583
]
},
{
"_id": "ad8af64a-d5bb-4162-9bb6-e5104126566d",
"company": "First Up Consultants",
"salesByStore": [
327583,
288582
]
},
{
"_id": "39acb3aa-f350-41cb-9279-9e34c004415a",
"company": "First Up Consultants",
"salesByStore": [
327583,
288582,
279183
]
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。