node.js - Perform this query on all documents in a collection: this.value = Math.min( this.max, this.value + this.increment) in mongodb -


my documents have {max, increment, value} , wish iterate on of them this.value = max( this.max + this.increment).

currently, find() of them, perform value = math.min( max, value + increment) on each one, save of them server (i in mongodb).

as can see, terrible way. want perform instruction documents, serverside only.

how 1 this? brownie whoever uses indexes further optimize or explains why indexes wouldn't help.

i using nodejs server , native mongodb library.

you create map function this:

db.yourcollection.mapreduce(mapfunctions...) 

http://docs.mongodb.org/manual/core/map-reduce/

"map-reduce operations can handle complex aggregation tasks. perform map-reduce operations, mongodb provides mapreduce command and, in mongo shell, db.collection.mapreduce() wrapper method."

http://docs.mongodb.org/manual/tutorial/map-reduce-examples/


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -