Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply field by value in Mongodb

Tags:

mongodb

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 Price, is it possible to do an update that sets Price to 50% off the existing value ?

So, given { Price : 19.99 }

I'd like to do db.collection.update({tag : "refurb"}, {$set {Price : Price * 0.50 }}, false, true);

Can this be done or do I have to read the value back to the client, modify, then update ? I guess the question then is can expressions be used in update, and can they reference the document being updated.

like image 648
Brad Avatar asked Dec 01 '11 14:12

Brad


People also ask

How do you multiply values in MongoDB?

MongoDB provides different types of arithmetic expression operators that are used in the aggregation pipeline stages and $multiply operator is one of them. This operator is used to multiply one number to another number and returns the result.

Which of these operator is used to multiply the value of a field by a number and to specify field in an embedded document?

The $mul is a field update operator that allows you to multiply the value of a field by a specified number. The $mul operator has the following syntax: { $mul: { <field1>: <number1>, <field2>: <number2>, ... } }

How do I round in MongoDB?

You can use the $round operator in MongoDB to round numeric values to a certain number of decimal places. This particular example rounds the values in the field “value” to one decimal place.


3 Answers

You can run server-side code with db.eval().

db.eval(function() {      db.collection.find({tag : "refurb"}).forEach(function(e) {         e.Price = e.Price * 0.5;         db.collection.save(e);     }); }); 

Note this will block the DB, so it's better to do find-update operation pair.

See https://docs.mongodb.com/manual/core/server-side-javascript/

like image 103
pingw33n Avatar answered Oct 09 '22 08:10

pingw33n


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: <number> } } 

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

db.collection.update(   { tag : "refurb"},   { $mul: { Price : 0.5 } } ); 
like image 41
Salvador Dali Avatar answered Oct 09 '22 07:10

Salvador Dali


Starting Mongo 4.2, db.collection.update() can accept an aggregation pipeline, finally allowing the update of a field based on another field:

// { price: 19.99 }
// { price: 2.04  }
db.collection.update(
  {},
  [{ $set: { price: { $multiply: [ 0.5, "$price" ] } } }],
  { multi: true }
)
// { price: 9.995 }
// { price: 1.02  }
  • The first part {} is the match query, filtering which documents to update (all documents in this case).

  • The second part [{ $set: { price: ... } }] is the update aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline). $set is a new aggregation operator and an alias of $addFields. Note how price is modified directly based on the its own value ($price).

  • Don't forget { multi: true }, otherwise only the first matching document will be updated.

like image 31
Xavier Guihot Avatar answered Oct 09 '22 07:10

Xavier Guihot