$push演算子は、ドキュメント内の配列に指定した値を追加するために使用されます。 $push演算子は、配列内の他の要素に影響を与えずに、既存の配列に新しい要素を追加します。
構文
db.collection.update({
< query >
}, {
$push: {
< field >: < value >
}
}, {
< options >
})
パラメーター
| パラメーター | Description |
|---|---|
<query> |
更新するドキュメントの選択基準。 |
<field> |
値の追加先となる配列フィールド。 |
<value> |
配列フィールドに追加する値。 |
<options> |
Optional. 更新操作の追加オプション。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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 - $setWindowFieldsでの$pushの使用
"First Up Consultants" 企業のすべての店舗で個別の販売量を取得するには、まずクエリを実行して社内のストアをパーティション分割します。 次に、$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
}
}])
このクエリによって返される最初の 3 つの結果は次のとおりです。
[
{
"_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 から Azure DocumentDB に移行するためのオプションを確認します。
- MongoDB との機能の互換性について詳しくは、こちらをご覧ください。