Added ~ field name substitution to mapreduce funcs
This commit is contained in:
		| @@ -477,16 +477,28 @@ class QuerySet(object): | |||||||
|         if not hasattr(self._collection, "map_reduce"): |         if not hasattr(self._collection, "map_reduce"): | ||||||
|             raise NotImplementedError("Requires MongoDB >= 1.1.1") |             raise NotImplementedError("Requires MongoDB >= 1.1.1") | ||||||
|  |  | ||||||
|         if not isinstance(map_f, pymongo.code.Code): |         map_f_scope = {} | ||||||
|             map_f = pymongo.code.Code(map_f) |         if isinstance(map_f, pymongo.code.Code): | ||||||
|         if not isinstance(reduce_f, pymongo.code.Code): |             map_f_scope = map_f.scope | ||||||
|             reduce_f = pymongo.code.Code(reduce_f) |             map_f = str(map_f) | ||||||
|  |         map_f = pymongo.code.Code(self._sub_js_fields(map_f), map_f_scope) | ||||||
|  |  | ||||||
|  |         reduce_f_scope = {} | ||||||
|  |         if isinstance(reduce_f, pymongo.code.Code): | ||||||
|  |             reduce_f_scope = reduce_f.scope | ||||||
|  |             reduce_f = str(reduce_f) | ||||||
|  |         reduce_f_code = self._sub_js_fields(reduce_f) | ||||||
|  |         reduce_f = pymongo.code.Code(reduce_f_code, reduce_f_scope) | ||||||
|  |  | ||||||
|         mr_args = {'query': self._query, 'keeptemp': keep_temp} |         mr_args = {'query': self._query, 'keeptemp': keep_temp} | ||||||
|  |  | ||||||
|         if finalize_f: |         if finalize_f: | ||||||
|             if not isinstance(finalize_f, pymongo.code.Code): |             finalize_f_scope = {} | ||||||
|                 finalize_f = pymongo.code.Code(finalize_f) |             if isinstance(finalize_f, pymongo.code.Code): | ||||||
|  |                 finalize_f_scope = finalize_f.scope | ||||||
|  |                 finalize_f = str(finalize_f) | ||||||
|  |             finalize_f_code = self._sub_js_fields(finalize_f) | ||||||
|  |             finalize_f = pymongo.code.Code(finalize_f_code, finalize_f_scope) | ||||||
|             mr_args['finalize'] = finalize_f |             mr_args['finalize'] = finalize_f | ||||||
|  |  | ||||||
|         if scope: |         if scope: | ||||||
|   | |||||||
| @@ -640,7 +640,7 @@ class QuerySetTest(unittest.TestCase): | |||||||
|         """ |         """ | ||||||
|         class BlogPost(Document): |         class BlogPost(Document): | ||||||
|             title = StringField() |             title = StringField() | ||||||
|             tags = ListField(StringField()) |             tags = ListField(StringField(), db_field='post-tag-list') | ||||||
|  |  | ||||||
|         BlogPost.drop_collection() |         BlogPost.drop_collection() | ||||||
|  |  | ||||||
| @@ -650,7 +650,7 @@ class QuerySetTest(unittest.TestCase): | |||||||
|  |  | ||||||
|         map_f = """ |         map_f = """ | ||||||
|             function() { |             function() { | ||||||
|                 this.tags.forEach(function(tag) { |                 this[~tags].forEach(function(tag) { | ||||||
|                     emit(tag, 1); |                     emit(tag, 1); | ||||||
|                 }); |                 }); | ||||||
|             } |             } | ||||||
| @@ -686,10 +686,10 @@ class QuerySetTest(unittest.TestCase): | |||||||
|         from time import mktime |         from time import mktime | ||||||
|  |  | ||||||
|         class Link(Document): |         class Link(Document): | ||||||
|             title = StringField() |             title = StringField(db_field='bpTitle') | ||||||
|             up_votes = IntField() |             up_votes = IntField() | ||||||
|             down_votes = IntField() |             down_votes = IntField() | ||||||
|             submitted = DateTimeField() |             submitted = DateTimeField(db_field='sTime') | ||||||
|  |  | ||||||
|         Link.drop_collection() |         Link.drop_collection() | ||||||
|  |  | ||||||
| @@ -726,8 +726,8 @@ class QuerySetTest(unittest.TestCase): | |||||||
|  |  | ||||||
|         map_f = """ |         map_f = """ | ||||||
|             function() { |             function() { | ||||||
|                 emit(this._id, {up_delta: this.up_votes - this.down_votes, |                 emit(this[~id], {up_delta: this[~up_votes] - this[~down_votes], | ||||||
|                                 sub_date: this.submitted.getTime() / 1000}) |                                 sub_date: this[~submitted].getTime() / 1000}) | ||||||
|             } |             } | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
| @@ -762,7 +762,8 @@ class QuerySetTest(unittest.TestCase): | |||||||
|  |  | ||||||
|         finalize_f = """ |         finalize_f = """ | ||||||
|             function(key, value) { |             function(key, value) { | ||||||
|                 // f(sec_since_epoch,y,z) = log10(z) + ((y*sec_since_epoch) / 45000) |                 // f(sec_since_epoch,y,z) =  | ||||||
|  |                 //                    log10(z) + ((y*sec_since_epoch) / 45000) | ||||||
|                 z_10 = Math.log(value.z) / Math.log(10); |                 z_10 = Math.log(value.z) / Math.log(10); | ||||||
|                 weight = z_10 + ((value.y * value.t_s) / 45000); |                 weight = z_10 + ((value.y * value.t_s) / 45000); | ||||||
|                 return weight; |                 return weight; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user