$mul Operator in MongoDB – Easy 2021 Guide

In this tutorial, I will discuss the $mul operator. The $mul operator allows us to multiply a given field’s value with a given number. Using the $mul operator, we can multiply a decimal value with a decimal number, a whole integer, and so on.

Since MongoDB 5.0, the $mul operator no longer raises an error when we pass an empty operand expression { } to the operator. The operation doesn’t change anything and is referred to as a no-op operation. It is an atomic operation that works only with a single document. If a field we referred to does not exist, the operator then creates that field and sets the value as 0 as the same numeric type of the multiplier.

The $mul Operator Syntax

Let us take a look the $mul operator syntax:

{ $mul: { field1: number1, ... } }

Using the $mul Operator in the Mongo Shell

Let us get started with the examples that we want to discuss using the $mul operator in MongoDB.

Multiplying Value of a Field with Decimal Number Using the $mul Operator

  • Start the MongoDB local database server
  • In the mongo she, pass the use db_name command to switch to a database
  • Let us look at the available documents for us in our sample database:
> db.shoes.find({}).pretty()

{
        "_id" : ObjectId("616b18a556e71503e4a0334d"),
        "name" : "Mariana Grider Boots",
        "price" : 23000,
        "size" : 7,
        "style" : "Slingbacks",
        "onSale" : false,
        "freeDelivery" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334e"),
        "name" : "Purple Shimmery Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Peep Toes",
        "onSale" : true,
        "freeDelivery" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a0334f"),
        "name" : "Zac Porter Straps",
        "price" : 19760,
        "size" : 8,
        "style" : "Ankle Strap",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03350"),
        "name" : "Valerina's Red Carpet Toes",
        "price" : 26000,
        "size" : 9,
        "style" : "Scarpin",
        "onSale" : false,
        "freeDelivery" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("616b18a556e71503e4a03351"),
        "name" : "Neeta Parmar's Stylish Bees 'n' Honey Boots",
        "price" : 10780,
        "size" : 7,
        "style" : "Wedge Booties",
        "onSale" : true,
        "freeDelivery" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83aca"),
        "name" : "Caroline Wielder Shoes",
        "price" : NumberDecimal("9999.99"),
        "size" : 5,
        "style" : "Wedge Booties",
        "onSale" : true,
        "freeDelivery" : false,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
{
        "_id" : ObjectId("6171bbf968378cae00d83acb"),
        "name" : "Marilyn Jaworski Sky Highs",
        "price" : NumberDecimal("12399.85"),
        "size" : 5,
        "style" : "Scarpin",
        "onSale" : false,
        "freeDelivery" : false,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}
  • Let us multiply the value of one of footwear by 1.5:
> db.shoes.update({"name" : "Caroline Wielder Shoes"}, {$mul: {price: NumberDecimal("1.5")}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


> db.shoes.find({"name" : "Caroline Wielder Shoes"}).pretty()
{
        "_id" : ObjectId("6171bbf968378cae00d83aca"),
        "name" : "Caroline Wielder Shoes",
        "price" : NumberDecimal("14999.985"),
        "size" : 5,
        "style" : "Wedge Booties",
        "onSale" : true,
        "freeDelivery" : false,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        }
}

Perfect! The $mul operator has multiplied the value of the price by 1.5.

Multiplying Value of an Embedded Field or Document

We will now multiply the value of an embedded field or document using the $mul operator.

> db.shoes.update({name: "Mariana Grider Boots"}, {$mul: {"moreDetails.qtyAvlbl": 4}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


> db.shoes.find({"name" : "Mariana Grider Boots"}).pretty()
{
        "_id" : ObjectId("616b18a556e71503e4a0334d"),
        "name" : "Mariana Grider Boots",
        "price" : 23000,
        "size" : 7,
        "style" : "Slingbacks",
        "onSale" : false,
        "freeDelivery" : true,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 40,
                "colors" : " "
        }
}

We have successfully multiplied the value of the embedded field.

Multiplying Value of a Missing or Non-Existent Field

We will now multiply a value of a missing or non-existent field using the $mul operator.

> db.shoes.update({name: "Purple Shimmery Heels"}, {$mul : {"itemNumber": NumberInt(500)}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })


> db.shoes.find({"name" : "Purple Shimmery Heels"}).pretty()
{
        "_id" : ObjectId("616b18a556e71503e4a0334e"),
        "name" : "Purple Shimmery Heels",
        "price" : 13380,
        "size" : 6,
        "style" : "Peep Toes",
        "onSale" : true,
        "freeDelivery" : false,
        "__v" : 0,
        "moreDetails" : {
                "qtyAvlbl" : 10,
                "colors" : " "
        },
        "itemNumber" : 0
}

The operator has created a new field and set its value as 0.

Read More: $unwind Operator in MongoDB: Easy Guide

Conclusion

In this article, we learned how to use the $mul operator in MongoDB.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 173