Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
işleci $push , belge içindeki bir diziye belirtilen değeri eklemek için kullanılır. $push işleci, dizideki diğer öğeleri etkilemeden var olan bir diziye yeni öğeler ekler.
Sözdizimi
db.collection.update({
< query >
}, {
$push: {
< field >: < value >
}
}, {
< options >
})
Parametreler
| Parametre | Description |
|---|---|
<query> |
Güncelleştirilecek belgelerin seçim ölçütleri. |
<field> |
Değerin eklendiği dizi alanı. |
<value> |
Dizi alanına eklenecek değer. |
<options> |
Optional. Güncelleştirme işlemi için ek seçenekler. |
Örnekler
Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.
{
"_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
}
]
}
]
}
Örnek 1 - Yeni satış kategorisi ekleme
salesByCategory dizisine yeni bir satış kategorisi eklemek için, alandaki $push işlecini kullanarak, kategori adı ve satış hacmine sahip yeni bir Sales nesnesiyle bir sorgu çalıştırın.
db.stores.update({
_id: "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"
}, {
$push: {
"sales.salesByCategory": {
categoryName: "Wine Accessories",
totalSales: 1000.00
}
}
})
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
]
Örnek 2 - $setWindowFields ile $push kullanma
"First Up Consultants" şirketi altındaki tüm mağazalardaki ayrı satış hacimlerini almak için önce şirket içindeki depoları bölümleme sorgusu çalıştırın. Ardından, $push işlecini kullanarak bölümdeki ilk mağazadan geçerli mağazaya kadar olan satışlar listesini oluşturun.
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
}
}])
Bu sorgu tarafından döndürülen ilk üç sonuç şunlardır:
[
{
"_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
]
}
]
İlgili içerik
- MongoDB'den Azure DocumentDB'ye geçiş seçeneklerini gözden geçirin.
- MongoDB ile özellik uyumluluğu hakkında daha fazla bilgi edinin.