$mul演算子は、フィールドの値に指定した数値を乗算します。 フィールドが存在しない場合は、 $mul フィールドが作成され、0 に設定されます。 この演算子は、パーセンテージの変更、スケーリング値の適用、または数値フィールドに対する一括計算の実行に役立ちます。
構文
{
$mul: {
<field1>: <number1>,
<field2>: <number2>,
...
}
}
パラメーター
| パラメーター | Description |
|---|---|
field |
乗算するフィールドの名前。 |
number |
フィールド値を乗算する数値。 数値にする必要があります。 |
例示
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: パーセンテージの増加を適用する
合計売上に 10% の増加を適用します (1.1 を掛けます)。 これにより、 totalSales が 24863 から 27349.3 (24863 × 1.1) に変更されます。
db.stores.updateOne(
{ "_id": "438db151-04b8-4422-aa97-acf94bc69cfc" },
{
$mul: {
"sales.totalSales": 1.1
}
}
)
例 2: 割引の適用
売上高に 20% 割引を適用します (0.8 を掛けます)。
db.stores.updateOne(
{ "_id": "438db151-04b8-4422-aa97-acf94bc69cfc" },
{
$mul: {
"sales.totalSales": 0.8
}
}
)
例 3: 複数のフィールド操作
複数のフィールドに異なる乗数を同時に適用します。
db.stores.updateOne(
{ "_id": "438db151-04b8-4422-aa97-acf94bc69cfc" },
{
$mul: {
"staff.totalStaff.fullTime": 1.5,
"staff.totalStaff.partTime": 2,
"sales.totalSales": 1.25
}
}
)
これにより、次の操作が行われます。
-
fullTimeを 8 から 12 に増やす (8 × 1.5) -
partTimeを 5 から 10 に増やす (5 × 2) -
totalSalesを 25% 増やす (1.25 を掛ける)
例 4: 新しいフィールドの作成
フィールドが存在しない場合は、フィールド $mul 作成し、0 に設定します。
db.stores.updateOne(
{ "_id": "438db151-04b8-4422-aa97-acf94bc69cfc" },
{
$mul: {
"sales.bonusPoints": 100,
"staff.overtimeHours": 40
}
}
)
bonusPointsとovertimeHoursの両方が値 0 で作成されます。
例 5: 10 進数の操作
財務計算に正確な 10 進数の乗数を適用します。
db.stores.updateOne(
{ "_id": "438db151-04b8-4422-aa97-acf94bc69cfc" },
{
$mul: {
"sales.totalSales": 0.915
}
}
)
例 6: 配列要素の更新
位置演算子を使用して、配列内の特定の要素に乗数を適用します。
db.stores.updateOne(
{
"_id": "438db151-04b8-4422-aa97-acf94bc69cfc",
"sales.salesByCategory.categoryName": "Direct-Drive Turntables"
},
{
$mul: {
"sales.salesByCategory.$.totalSales": 1.15
}
}
)
例 7: 負の値とゼロ
負の乗数とゼロ値を処理します。
db.stores.updateOne(
{ "_id": "438db151-04b8-4422-aa97-acf94bc69cfc" },
{
$mul: {
"sales.totalSales": -1,
"staff.totalStaff.fullTime": 0
}
}
)
これにより、次の操作が行われます。
-
totalSales負の値にする (反転に役立つ) -
fullTimeを 0 に設定
スタッフに 1.5 乗数を適用し、売上に 1.25 を適用すると、ドキュメントは次のように更新されます。
{
"_id": "438db151-04b8-4422-aa97-acf94bc69cfc",
"name": "Fourth Coffee | Turntable Boutique - Tromptown",
"staff": {
"totalStaff": {
"fullTime": 12,
"partTime": 10
},
"overtimeHours": 0
},
"sales": {
"totalSales": 31078.75,
"bonusPoints": 0,
"salesByCategory": [
{
"categoryName": "Direct-Drive Turntables",
"totalSales": 28592.45
}
]
}
}