Multiply field by value in Mongodb

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/

Leave a Comment