node.js - mongoose - divide and round -
hey question simple can't figure out: have huge mongoose collection player
field money
. how can divide value 100 , have round number (no decimal)
i tried
players.update({}, {$mul: {'money':.01}}, {multi: true}).exec()
it works dividing 100 don't know how round it?
by way, why isn't there documentation $mul
operator on official mongoose documentation ? http://mongoosejs.com/docs/3.0.x/docs/api.html
thanks folks
currently mongodb doesn't have in-built operators support math.ceil()
or math.floor()
functions. need on application server or write javascript function resides on database server , same.
implementing in application code:
players.find().foreach(function(doc){ var updated = math.ceil(doc.money/100); players.update({"_id":doc._id},{$set:{"money":updated}}); })
implementing server side javascript function:
db.system.js.insert({"_id":"updatemoney","value":function(num){ db.players.find().foreach(function(doc){ var updated = math.ceil(doc.money/num); db.players.update({"_id":doc._id},{$set:{"money":updated}}); }) }})
and invoking function mongoose:
mongoose.connection.on("open", function(err){ mongoose.connection.db.eval("updatemoney(100)", function (err, retval) { console.log('err: '+err); console.log('retval: '+retval); }); });
$mul mongodb operator, mongoose api enables use these operators query mongodb.
Comments
Post a Comment