MongoDB – Multiply Operator ($mul)

MongoDB provides different types of field update operators to update the values of the fields of the documents and $mul operator is one of them. This operator is used to multiply the value of the field by a number.

  • $mul operator only updates those fields whose value are of numeric type like int, float, etc.
  • If the specified field is not present in the document, then this operator will add that field in the document and assign the value of that field to zero of the same numeric type as the multiplier.
  • This operator is an atomic operation within a single document.
  • In this operator, multiplication with values of mixed numeric types like 32-bit integer, 64-bit integer, float, etc., may result in conversion of numeric type. The following rules are applied in the multiplication with the values of mixed numeric types:
    32-bit Integer 64-bit Integer Float
    32-bit Integer 32-bit or 64-bit Integer 64-bit Integer Float
    64-bit Integer 64-bit Integer 64-bit Integer Float
    Float Float Float Float
  • $mul operator can also work with embedded/nested documents or arrays. You can use this operator in methods like update(), findAndModify(), etc., according to your requirements.

    Syntax:

    { $mul: { <field1>: <number1>, <field2>: <number2>, ... } }

    Here, <field> can specify with dot notation in embedded/nested documents.

    In the following examples, we are working with:



    Database: Fruits
    Collection: Details
    Document: two documents that contain the details of the fruits in the form of field-value pairs.

    Multiplying the value of a field:

    In this example, we are multiplying the value of price field by 2.10 in the document who matches the specified condition, i.e., name = mango.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    db.Details.update({name: "mango"}, {$mul: {price: NumberDecimal("2.10")}})

    chevron_right

    
    

    Multiplying the value of a field in the embedded/nested document:

    In this example, we are multiplying the value of quantity.tQuantity field by 3 in the document who matches the specified condition, i.e., name = mango.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    db.Details.update({name: "mango"}, {$mul: {"quantity.tQuantity": 3}})

    chevron_right

    
    

    Using $mul operator to a non-existing field:

    In this example, we are applying $mul operator to a non- existing field in the document who matches the specified condition, i.e., name = apple.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    db.Details.update({name: "apple"}, {$mul: {"batchNumber":NumberInt(230)}})

    chevron_right

    
    

    Using $mul operator to multiply mixed numeric type:

    In this example, we are multiplying the value(float type) of price field in the document who matches the specified condition, i.e., name = apple.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    db.Details.update({name: "apple"}, {$mul: {price: NumberDecimal(5)}})

    chevron_right

    
    




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.