Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Operator $each digunakan dalam operasi $addToSet atau $push untuk menambahkan beberapa elemen ke bidang array dalam satu operasi pembaruan. Operator ini berguna ketika Anda perlu menyisipkan beberapa item ke dalam array tanpa harus melakukan beberapa operasi pembaruan. Operator $each memastikan bahwa setiap item dalam array yang ditentukan ditambahkan ke array target.
Syntax
{
$push: {
<field>: {
$each: [ <value1>, <value2>],
<modifier1>: <value1>,
<modifier2>: <value2>
}
}
}
Parameter-parameternya
| Pengaturan | Description |
|---|---|
<field> |
Bidang yang akan diperbarui. |
$each |
Array nilai yang akan ditambahkan ke bidang array. |
<modifier> |
Pengubah opsional seperti $sort, , dan $slice untuk mengontrol perilaku $position$pushoperasi. |
Examples
Pertimbangkan dokumen sampel ini dari koleksi toko.
{
"_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
}
]
}
]
}
Contoh 1: Menambahkan beberapa elemen ke array
Kueri ini menambahkan beberapa peristiwa promosi baru ke promotionEvents array.
db.stores.updateOne({
name: "Lenore's New DJ Equipment Store"
}, {
$push: {
promotionEvents: {
$each: [{
eventName: "Grand Savings",
promotionalDates: {
startDate: "2024-08-01",
endDate: "2024-08-31"
},
discounts: [{
categoryName: "DJ Headphones",
discountPercentage: 5
}]
},
{
eventName: "Big Bargain",
promotionalDates: {
startDate: "2024-11-25",
endDate: "2024-11-30"
},
discounts: [{
categoryName: "DJ Headphones",
discountPercentage: 20
}]
}
]
}
}
})
Kueri ini mengembalikan hasil berikut.
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "0",
"modifiedCount": "0",
"upsertedCount": 0
}
]
Konten terkait
- Tinjau opsi untuk bermigrasi dari MongoDB ke Azure DocumentDB.
- Baca selengkapnya tentang kompatibilitas fitur dengan MongoDB.