Multiply field by value in Mongodb

后端 未结 4 887
旧时难觅i
旧时难觅i 2020-12-01 03:15

I\'ve been looking for a way to create an update statement that will take an existing numeric field and modify it using an expression. For example, if I have a field called

4条回答
  •  攒了一身酷
    2020-12-01 03:23

    In the new Mongo 2.6.x there is a $mul operator. It would multiply the value of the field by the number with the following syntax.

    {
      $mul: { field:  }
    }
    

    So in your case you will need to do the following:

    db.collection.update(
      { tag : "refurb"},
      { $mul: { Price : 0.5 } }
    );
    

提交回复
热议问题