$mul
Multiplies the value of a field by a specified number, creating the field with value zero if it doesn't exist, useful for scaling and percentage operations.
Syntax
{ $mul: { <field1>: <number1>, <field2>: <number2> } }
Parameters
field
stringrequiredThe name of the field to multiply
number
numberrequiredThe number to multiply the field value by
Examples
Apply percentage increase
Apply a 10% increase to total sales (multiply by 1.1)
Query:
db.stores.updateOne({ "_id": "store-001" }, { $mul: { "sales.totalSales": 1.1 } })
Output:
Increases totalSales by 10% through multiplication
Apply discount
Apply a 20% discount to sales figures (multiply by 0.8)
Query:
db.stores.updateOne({ "_id": "store-001" }, { $mul: { "sales.totalSales": 0.8 } })
Output:
Reduces totalSales by 20% through multiplication
Multiple field operations
Apply different multipliers to multiple fields simultaneously
Query:
db.stores.updateOne({ "_id": "store-001" }, { $mul: { "staff.totalStaff.fullTime": 1.5, "staff.totalStaff.partTime": 2, "sales.totalSales": 1.25 } })
Output:
Applies different scaling factors to staff counts and sales