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.
$ Konum işleci, dizinindeki öğenin konumunu açıkça belirtmeden güncelleştirilecek bir dizideki bir öğeyi tanımlar. işleci, $ sorgu koşuluyla eşleşen ilk öğe için yer tutucu işlevi görür ve dizi alanının sorgu belgesinin bir parçası olarak görünmesi gerekir.
Sözdizimi
db.collection.updateOne(
{ <array>: <value> },
{ <update operator>: { "<array>.$": <value> } }
)
Parametreler
| Parametre | Description |
|---|---|
array |
Güncelleştirilecek öğeyi içeren dizi alanı. Sorgu koşulunun bir parçası olmalıdır. |
value |
Sorgu koşulundaki dizi öğesiyle eşleştirmek için kullanılan değer. |
update operator |
Uygulanacak güncelleştirme işleci (örneğin, $set, $inc, $unset). |
Ö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: Bir dizinin koşulla eşleşen ilk öğesini yansıtma
Bu sorgu, 35000'den büyük donanımlar salesByCategory için DJ dizideki totalSales ilk öğeyi döndürür.
db.stores.find({
"sales.salesByCategory": {
$elemMatch: {
categoryName: {
$regex: "^DJ"
}
}
},
"sales.salesByCategory.totalSales": {
$gt: 35000
}
}, {
"sales.salesByCategory.$": 1
}).limit(2)
Bu sorgu tarafından döndürülen ilk iki sonuç şunlardır:
[
{
"_id": "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45",
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Speakers",
"totalSales": 36972
}
]
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35911
}
]
}
}
]
Örnek 2: Belirli bir kategori için indirim yüzdesini güncelleştirme
Bu sorgu, ilk eşleşen yükseltme olayında "Masalar" kategorisi için indirim yüzdesini güncelleştirir.
db.stores.updateOne(
{
_id: "905d1939-e03a-413e-a9c4-221f74055aac",
"promotionEvents.discounts.categoryName": "Desks"
},
{
$set: { "promotionEvents.$.discounts.$[elem].discountPercentage": 25 }
},
{
arrayFilters: [{ "elem.categoryName": "Desks" }]
}
)
Örnek 3: Satış kategorisi toplamlarını güncelleştirme
Bu sorgu, kullanarak belirli bir kategori için toplam satışları $ (positional operator)güncelleştirir.
db.stores.updateOne(
{
_id: "905d1939-e03a-413e-a9c4-221f74055aac",
"sales.salesByCategory.categoryName": "Desk Lamps"
},
{
$inc: { "sales.salesByCategory.$.totalSales": 1000 }
}
)