Multiply field by value in Mongodb

后端 未结 4 891
旧时难觅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:43

    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/

提交回复
热议问题