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.
Toplama işlem hattındaki $addFields aşaması, belgelere yeni alanlar eklemek için kullanılır. Mevcut alanların değerlerini sıfırlamak için de kullanılabilir. Bu aşama, var olan verileri temel alan yeni alanlar oluşturmanız veya belgelerinizdeki mevcut alanları değiştirmeniz gerektiğinde özellikle yararlıdır.
Sözdizimi
{
$addFields: {
<newField1>: <expression1>,
<newField2>: <expression2>,
...
}
}
Parametreler
| Parametre | Description |
|---|---|
newField1 |
Eklenecek yeni alanın adı veya değiştirileceği mevcut alan. |
expression1 |
newField1 değerini hesaplamak için ifade. |
Ö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 alan ekleme
Bu sorgu, yükseltme olaylarının sayısını sayan yeni bir totalDiscountEvents alanı ekler
db.stores.aggregate([
{
$addFields: {
totalDiscountEvents: { $size: "$store.promotionEvents" }
}
}
])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": ["Summer Sale", "Black Friday", "Holiday Deals"]
},
"totalDiscountEvents": 3
}
]
Örnek 2: Varolan Bir Alanı Değiştirme
Bu sorgu, tam zamanlı ve yarı zamanlı personeli toplayan totalStaffCount alanını ekler.
db.stores.aggregate([
{
$addFields: {
totalStaffCount: {
$add: ["$store.staff.totalStaff.fullTime", "$store.staff.totalStaff.partTime"]
}
}
}
])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"staff": {
"totalStaff": {
"fullTime": 12,
"partTime": 8
}
}
},
"totalStaffCount": 20
}
]
Örnek 3: İç İçe Alanlar Ekleme
Bu sorgu, enlem ve boylamı bir dizide birleştiren iç içe yerleştirilmiş bir location.coordinates alanı ekler.
db.stores.aggregate([
{
$addFields: {
"store.location.coordinates": ["$store.location.lat", "$store.location.lon"]
}
}
])
Bu sorgu aşağıdaki sonucu döndürür:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"location": {
"lat": 47.6097,
"lon": -122.3331,
"coordinates": [47.6097, -122.3331]
}
}
}
]