$位置指定演算子は、配列内の要素の位置を明示的に指定せずに、更新する配列内の要素を識別します。
$演算子は、クエリ条件に一致する最初の要素のプレースホルダーとして機能し、配列フィールドはクエリ ドキュメントの一部として表示される必要があります。
構文
db.collection.updateOne(
{ <array>: <value> },
{ <update operator>: { "<array>.$": <value> } }
)
パラメーター
| パラメーター | Description |
|---|---|
array |
更新する要素を含む配列フィールド。 クエリ条件の一部である必要があります。 |
value |
クエリ条件の配列要素と一致するために使用される値。 |
update operator |
適用する更新演算子 (たとえば、 $set、 $inc、 $unset)。 |
例示
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が 35000 を超えるDJ機器に対して、totalSales配列内の最初の要素を返します。
db.stores.find({
"sales.salesByCategory": {
$elemMatch: {
categoryName: {
$regex: "^DJ"
}
}
},
"sales.salesByCategory.totalSales": {
$gt: 35000
}
}, {
"sales.salesByCategory.$": 1
}).limit(2)
このクエリによって返される最初の 2 つの結果は次のとおりです。
[
{
"_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
}
]
}
}
]
例 2: 特定のカテゴリの割引率を更新する
このクエリは、最初に一致するプロモーション イベントの "Desks" カテゴリの割引率を更新します。
db.stores.updateOne(
{
_id: "905d1939-e03a-413e-a9c4-221f74055aac",
"promotionEvents.discounts.categoryName": "Desks"
},
{
$set: { "promotionEvents.$.discounts.$[elem].discountPercentage": 25 }
},
{
arrayFilters: [{ "elem.categoryName": "Desks" }]
}
)
例 3: 売上カテゴリの合計を更新する
このクエリでは、 $ (positional operator)を使用して、特定のカテゴリの売上合計が更新されます。
db.stores.updateOne(
{
_id: "905d1939-e03a-413e-a9c4-221f74055aac",
"sales.salesByCategory.categoryName": "Desk Lamps"
},
{
$inc: { "sales.salesByCategory.$.totalSales": 1000 }
}
)